Java Practice Test - 5

Java Practice Test - 5

25 Questions Published

Questions

Question 1 Multiple Choice (Single Answer)

What will be the output of the program?
int I = 0;
label:
if (I < 2) {
System.out.print("I is " + I);
I++;
continue label;
}

  1. I is 0
  2. I is 0 I is 1
  3. Compilation error
  4. None of the above
Question 2 Multiple Choice (Single Answer)

class Test1
{
public int value;
public int hashCode() { return 42; }
}
class Test2
{
public int value;
public int hashcode() { return (int)(value^5); }
}
which statement is true?

  1. class Test1 will not compile.
  2. The Test1 hashCode() method is more efficient than the Test2 hashCode() method.
  3. The Test1 hashCode() method is less efficient than the Test2 hashCode()method.
  4. class Test2 will not compile.
Question 3 Multiple Choice (Single Answer)

What will be the output of the program?
int i = 0, j = 5;
tp: for (;;)
{
i++;
for (;;)
{
if(i > --j)
{
break tp;
}
}
System.out.println("i =" + i + ", j = " + j);

  1. i = 1, j = 0
  2. i = 1, j = 4
  3. i = 3, j = 4
  4. Compilation fails.
Question 4 Multiple Choice (Single Answer)

Which statement is true?

  1. A try statement must have at least one corresponding catch block.
  2. Multiple catch statements can catch the same class of exception more than once.
  3. An Error that might be thrown in a method must be declared as thrown by that method, or be handled within that method.
  4. Except in case of VM shutdown, if a try block starts to execute, a corresponding finally block will always start to execute.
Question 5 Multiple Choice (Single Answer)

class Bar { }
class Test
{
Bar doBar()
{
Bar b = new Bar(); /* Line 6 /
return b; /
Line 7 /
}
public static void main (String args[])
{
Test t = new Test(); /
Line 11 /
Bar newBar = t.doBar(); /
Line 12 /
System.out.println("newBar");
newBar = new Bar(); /
Line 14 /
System.out.println("finishing"); /
Line 15 */
}
}
At what point is the Bar object, created on line 6, eligible for garbage collection?

  1. after line 12
  2. after line 14
  3. after line 7, when doBar() completes
  4. after line 15, when main() completes
Question 6 Multiple Choice (Single Answer)

What will be the output of the program?
try
{
int x = 0;
int y = 5 / x;
}
catch (Exception e)
{
System.out.println("Exception");
}
catch (ArithmeticException ae)
{
System.out.println(" Arithmetic Exception");
}
System.out.println("finished");

  1. finished
  2. Exception
  3. Compilation fails
  4. Arithmetic Exception
Question 7 Multiple Choice (Single Answer)

What will be the output of the program?
public class Foo
{
Foo()
{
System.out.print("foo");
}

class Bar
{
Bar()
{
System.out.print("bar");
}
public void go()
{
System.out.print("hi");
}
} /* class Bar ends */

public static void main (String [] args) 
{
    Foo f = new Foo();
    f.makeBar();
}
void makeBar() 
{
    (new Bar() {}).go();
}

}/* class Foo ends */

  1. Compilation fails.
  2. An error occurs at runtime.
  3. It prints "foobarhi"
  4. It prints "barhi"
Question 8 Multiple Choice (Single Answer)

What will be the output of the program?
String a = "newspaper";
a = a.substring(5,7);
char b = a.charAt(1);
a = a + b;
System.out.println(a);

  1. apa
  2. app
  3. apea
  4. apep
Question 9 Multiple Choice (Single Answer)

class Test
{
private Demo d;
void start()
{
d = new Demo();
this.takeDemo(d); /* Line 7 /
} /
Line 8 */
void takeDemo(Demo demo)
{
demo = null;
demo = new Demo();
}
}
When is the Demo object eligible for garbage collection?

  1. After line 7
  2. After line 8
  3. After the start() method completes
  4. When the instance running this code is made eligible for garbage collection.
Question 10 Multiple Choice (Single Answer)

public class Test { }
What is the prototype of the default constructor?

  1. Test( )
  2. Test(void)
  3. public Test( )
  4. public Test(void)
Question 11 Multiple Choice (Single Answer)

public class X
{
public static void main(String [] args)
{
X x = new X();
X x2 = m1(x); /* Line 6 /
X x4 = new X();
x2 = x4; /
Line 8 */
doComplexStuff();
}
static X m1(X mx)
{
mx = new X();
return mx;
}
}
After line 8 runs. how many objects are eligible for garbage collection?

  1. 0
  2. 1
  3. 2
  4. 3
Question 12 Multiple Choice (Single Answer)

What will be the output of the program?
public class SqrtExample
{
public static void main(String [] args)
{
double value = -9.0;
System.out.println( Math.sqrt(value));
}
}

  1. 3.0
  2. -3.0
  3. NaN
  4. Compilation fails.
Question 13 Multiple Choice (Single Answer)

What will be the output of the program?
public class Test
{
public static void main(String args[])
{
class Foo
{
public int i = 3;
}
Object o = (Object)new Foo();
Foo foo = (Foo)o;
System.out.println("i = " + foo.i);
}
}

  1. i = 3
  2. Compilation fails.
  3. i = 5
  4. A ClassCastException will occur.
Question 14 Multiple Choice (Single Answer)

What is the narrowest valid returnType for methodA in line 3?
public class ReturnIt
{
returnType methodA(byte x, double y) /* Line 3 */
{
return (long)x / y * 2;
}
}

  1. int
  2. byte
  3. long
  4. double
Question 15 Multiple Choice (Single Answer)

Which two cause a compiler error?
float[ ] f = new float(3);
float f2[ ] = new float[ ];
float[ ]f1 = new float[3];
float f3[ ] = new float[3];
float f5[ ] = {1.0f, 2.0f, 2.0f};

  1. 2, 4
  2. 3, 5
  3. 4, 5
  4. 1, 2
Question 16 Multiple Choice (Single Answer)

What will be the output of the program?
public class A
{
void A() /* Line 3 */
{
System.out.println("Class A");
}
public static void main(String[] args)
{
new A();
}
}

  1. Class A
  2. Compilation fails.
  3. An exception is thrown at line 3.
  4. The code executes with no output.
Question 17 Multiple Choice (Single Answer)

public class Test
{
public void foo()
{
assert false; /* Line 5 /
assert false; /
Line 6 /
}
public void bar()
{
while(true)
{
assert false; /
Line 12 /
}
assert false; /
Line 14 */
}
}
What causes compilation to fail?

  1. Line 5
  2. Line 6
  3. Line 12
  4. Line 14
Question 18 Multiple Choice (Single Answer)

What will be the output of the program?
class Test
{
public static void main(String [] args)
{
int x= 0;
int y= 0;
for (int z = 0; z < 5; z++)
{
if (( ++x > 2 ) || (++y > 2))
{
x++;
}
}
System.out.println(x + " " + y);
}
}

  1. 5 3
  2. 8 2
  3. 8 3
  4. 8 5
Question 19 Multiple Choice (Single Answer)

What will be the output of the program?
public class MyProgram
{
public static void main(String args[])
{
try
{
System.out.print("Hello world ");
}
finally
{
System.out.println("Finally executing ");
}
}
}

  1. Nothing. The program will not compile because no exceptions are specified.
  2. Nothing. The program will not compile because no catch clauses are specified.
  3. Hello world.
  4. Hello world Finally executing
Question 20 Multiple Choice (Single Answer)

You want subclasses in any package to have access to members of a superclass. Which is the most restrictive access that accomplishes this objective?

  1. public
  2. private
  3. protected
  4. transient
Question 21 Multiple Choice (Single Answer)

class A
{
protected int method1(int a, int b)
{
return 0;
}
}
Which is valid in a class that extends class A?

  1. public int method1(int a, int b) {return 0; }
  2. private int method1(int a, int b) { return 0; }
  3. public short method1(int a, int b) { return 0; }
  4. static protected int method1(int a, int b) { return 0; }
Question 22 Multiple Choice (Single Answer)

interface Base
{
boolean m1 ();
byte m2(short s);
}
which two code fragments will compile?
interface Base2 implements Base {}
abstract class Class2 extends Base
{ public boolean m1(){ return true; }}
abstract class Class2 implements Base {}
abstract class Class2 implements Base
{ public boolean m1(){ return (7 > 4); }}
abstract class Class2 implements Base
{ protected boolean m1(){ return (5 > 7) }}

  1. 1 and 2
  2. 2 and 3
  3. 3 and 4
  4. 1 and 5
Question 23 Multiple Choice (Single Answer)

import java.awt.Button;
class CompareReference
{
public static void main(String [] args)
{
float f = 42.0f;
float [] f1 = new float[2];
float [] f2 = new float[2];
float [] f3 = f1;
long x = 42;
f1[0] = 42.0f;
}
}
which three statements are true?
f1 == f2
f1 == f3
f2 == f1[1]
x == f1[0]
f == f1[0

  1. 1, 2 and 3
  2. 2, 4 and 5
  3. 3, 4 and 5
  4. 1, 4 and 5
Question 24 Multiple Choice (Single Answer)

What will be the output of the program?
class Test
{
public static void main(String [] args)
{
Test p = new Test();
p.start();
}

void start() 
{
    boolean b1 = false;
    boolean b2 = fix(b1);
    System.out.println(b1 + &quot; &quot; + b2);
}

boolean fix(boolean b1) 
{
    b1 = true;
    return b1;
}

}

  1. true true
  2. false true
  3. true false
  4. false false
Question 25 Multiple Choice (Single Answer)

What will be the output of the program?
class PassS
{
public static void main(String [] args)
{
PassS p = new PassS();
p.start();
}

void start() 
{
    String s1 = &quot;slip&quot;;
    String s2 = fix(s1);
    System.out.println(s1 + &quot; &quot; + s2);
}

String fix(String s1) 
{
    s1 = s1 + &quot;stream&quot;;
    System.out.print(s1 + &quot; &quot;);
    return &quot;stream&quot;;
}

}

  1. slip stream
  2. slipstream stream
  3. stream slip stream
  4. slipstream slip stream