Java Core Fundamentals - Practice Test 6

Practice test covering Java language fundamentals including control flow, exceptions, OOP concepts, collections, threading, and core syntax.

25 Questions Published

Questions

Question 1 Multiple Choice (Single Answer)

public void test(int x)
{
int odd = 1;
if(odd) /* Line 4 */
{
System.out.println(odd);
}
else
{
System.out.println(even);
}
}


Which statement is true?

  1. Compilation fails.
  2. odd will always be the output.
  3. even will always be the output.
  4. odd will be the output for odd values of x, and even for even values.
Question 2 Multiple Choice (Single Answer)

import java.awt.;
class Ticker extends Component
{
public static void main (String [] args)
{
Ticker t = new Ticker();
/
Missing Statements ? */
}
}
which two of the following statements, inserted independently, could legally be inserted into missing section of this code?
boolean test = (Component instanceof t);
boolean test = (t instanceof Ticker);
boolean test = t.instanceof(Ticker);
boolean test = (t instanceof Component);

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

What will be the output of the program?
int i = 1, j = 10;
do
{
if(i > j)
{
break;
}
j--;
} while (++i < 5);
System.out.println("i = " + i + " and j = " + j);

  1. i = 6 and j = 5
  2. i = 5 and j = 5
  3. i = 6 and j = 4
  4. i = 5 and j = 6
Question 4 Multiple Choice (Single Answer)

What will be the output of the program?

public class Test
{
public static void main(String args[])
{
int i = 1, j = 0;
switch(i)
{
case 2: j += 6;
case 4: j += 1;
default: j += 2;
case 0: j += 4;
}
System.out.println(j = + j);
}
}

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

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

void start() 
{
    int x = 7;
    twice(x);
    System.out.print(x + &quot; &quot;);
}

void twice(int x) 
{
    x = x*2;
    s = x;
}

}

  1. 7 7
  2. 7 14
  3. 14 0
  4. 14 14
Question 6 Multiple Choice (Single Answer)

Which statement is true?

  1. catch(X x) can catch subclasses of X where X is a subclass of Exception.
  2. The Error class is a RuntimeException.
  3. Any statement that can throw an Error must be enclosed in a try block.
  4. Any statement that can throw an Exception must be enclosed in a try block.
Question 7 Multiple Choice (Single Answer)

Which is valid declaration of a float?

  1. float f = 1F;
  2. float f = 1.0;
  3. float f = "1";
  4. float f = 1.0d;
Question 8 Multiple Choice (Single Answer)

Which will contain the body of the thread?

  1. run();
  2. start();
  3. stop();
  4. main();
Question 9 Multiple Choice (Single Answer)

What will be the output of the program?
import java.util.*;
class H
{
public static void main (String[] args)
{
Object x = new Vector().elements();
System.out.print((x instanceof Enumeration)+",");
System.out.print((x instanceof Iterator)+",");
System.out.print(x instanceof ListIterator);
}
}

  1. Prints: false,false,false
  2. Prints: false,false,true
  3. Prints: false,true,false
  4. Prints: true,false,false
Question 10 Multiple Choice (Single Answer)

What will be the output of the program?
TreeSet map = new TreeSet();
map.add("one");
map.add("two");
map.add("three");
map.add("four");
map.add("one");
Iterator it = map.iterator();
while (it.hasNext() )
{
System.out.print( it.next() + " " );
}

  1. one two three four
  2. four three two one
  3. four one three two
  4. one two three four one
Question 11 Multiple Choice (Single Answer)

import java.io.*;
public class MyProgram
{
public static void main(String args[])
{
FileOutputStream out = null;
try
{
out = new FileOutputStream("test.txt");
out.write(122);
}
catch(IOException io)
{
System.out.println("IO Error.");
}
finally
{
out.close();
}
}
}
and given that all methods of class FileOutputStream, including close(), throw anIOException, which of these is true?

  1. This program will compile successfully.
  2. This program fails to compile due to an error at line 4.
  3. This program fails to compile due to an error at line 6.
  4. This program fails to compile due to an error at line 13.
Question 12 Multiple Choice (Single Answer)

Which three guarantee that a thread will leave the running state?
yield()
wait()
notify()
notifyAll()
sleep(1000)
aLiveThread.join()
Thread.killThread()

  1. 1, 2 and 4
  2. 2, 5 and 6
  3. 3, 4 and 7
  4. 4, 5 and 7
Question 13 Multiple Choice (Single Answer)

interface DoMath
{
double getArea(int rad);
}
interface MathPlus
{
double getVol(int b, int h);
}
/* Missing Statement(s) ? */
Which code fragment(s) inserted at end of the program, will allow to compile?

  1. class AllMath extends DoMath { double getArea(int r); }
  2. interface AllMath implements MathPlus { double getVol(int x, int y); }
  3. interface AllMath extends DoMath { float getAvg(int h, int l); }
  4. class AllMath implements MathPlus { double getArea(int rad); }
  5. abstract class AllMath implements DoMath, MathPlus { public double getArea(int rad) { return rad * rad * 3.14; } }
  1. 1 only
  2. 2 only
  3. 3 and 5
  4. 1 and 4
Question 14 Multiple Choice (Single Answer)

class Foo
{
class Bar{ }
}
class Test
{
public static void main (String [] args)
{
Foo f = new Foo();
/* Line 10: Missing statement ? */
}
}
which statement, inserted at line 10, creates an instance of Bar?

  1. Foo.Bar b = new Foo.Bar();
  2. Foo.Bar b = f.new Bar();
  3. Bar b = new f.Bar();
  4. Bar b = f.new Bar();
Question 15 Multiple Choice (Single Answer)

Which two statements are equivalent?

  1. 16*4
  2. 16>>2
  3. 16/2^2
  4. 16>>>2
  1. 1 and 2
  2. 2 and 4
  3. 3 and 5
  4. 1 and 3
Question 16 Multiple Choice (Single Answer)

What will be the output of the program?
class Test
{
public static void main(String [] args)
{
int x=20;
String sup = (x < 15) ? "small" : (x < 22)? "tiny" : "huge";
System.out.println(sup);
}
}

  1. small
  2. tiny
  3. huge
  4. Compilation fails
Question 17 Multiple Choice (Single Answer)

Given a method in a protected class, what access modifier do you use to restrict access to that method to only the other members of the same class?

  1. final
  2. static
  3. private
  4. protected
  5. volatile
Question 18 Multiple Choice (Single Answer)

What will be the output of the program?
public class BoolTest
{
public static void main(String [] args)
{
int result = 0;

    Boolean b1 = new Boolean(&quot;TRUE&quot;);
    Boolean b2 = new Boolean(&quot;true&quot;);
    Boolean b3 = new Boolean(&quot;tRuE&quot;);
    Boolean b4 = new Boolean(&quot;false&quot;);

    if (b1 == b2)  /* Line 10 */
        result = 1;
    if (b1.equals(b2) ) /* Line 12 */
        result = result + 10;
    if (b2 == b4)  /* Line 14 */
        result = result + 100;
    if (b2.equals(b4) ) /* Line 16 */
        result = result + 1000;
    if (b2.equals(b3) ) /* Line 18 */
        result = result + 10000;

    System.out.println(&quot;result = &quot; + result);
}

}

  1. 0
  2. 1
  3. 10
  4. 10010
Question 19 Multiple Choice (Single Answer)

Which three statements are true?

  1. The default constructor initialises method variables.
  2. The default constructor has the same access as its class.
  3. The default constructor invokes the no-arg constructor of the superclass.
  4. If a class lacks a no-arg constructor, the compiler always creates a default constructor.
  5. The compiler creates a default constructor only when there are no other constructors for the class.
  1. 1, 2 and 4
  2. 2, 3 and 5
  3. 3, 4 and 5
  4. 1, 2 and 3
Question 20 Multiple Choice (Single Answer)

Which of the following are Java reserved words?

  1. run
  2. import
  3. default
  4. implement
  1. 1 and 2
  2. 2 and 3
  3. 3 and 4
  4. 2 and 4
Question 21 Multiple Choice (Single Answer)

What will be the output of the program?

public class Test
{
public static void main(String [] args)
{
int I = 1;
do while ( I < 1 )
System.out.print(I is + I);
while ( I > 1 ) ;
}
}

  1. I is 1
  2. I is 1 I is 1
  3. No output is produced
  4. Compilation error
Question 22 Multiple Choice (Single Answer)

Which of the following are legal lines of code?

  1. int w = (int)888.8;
  2. byte x = (byte)1000L;
  3. long y = (byte)100;
  4. byte z = (byte)100L;
  1. 1 and 2
  2. 2 and 3
  3. 3 and 4
  4. All statements are correct.
Question 23 Multiple Choice (Single Answer)

What will be the output of the program?
public class Test
{
public static void main(String[] args)
{
int x = 0;
assert (x > 0) ? "assertion failed" : "assertion passed" ;
System.out.println("finished");
}
}

  1. finished
  2. Compiliation fails.
  3. An AssertionError is thrown and finished is output.
  4. An AssertionError is thrown with the message "assertion failed."
Question 24 Multiple Choice (Single Answer)

What will be the output of the program?
public class RTExcept
{
public static void throwit ()
{
System.out.print("throwit ");
throw new RuntimeException();
}
public static void main(String [] args)
{
try
{
System.out.print("hello ");
throwit();
}
catch (Exception re )
{
System.out.print("caught ");
}
finally
{
System.out.print("finally ");
}
System.out.println("after ");
}
}

  1. hello throwit caught
  2. Compilation fails
  3. hello throwit RuntimeException caught after
  4. hello throwit caught finally after
Question 25 Multiple Choice (Single Answer)

What will be the output of the program?
public class X
{
public static void main(String [] args)
{
try
{
badMethod();
System.out.print("A");
}
catch (Exception ex)
{
System.out.print("B");
}
finally
{
System.out.print("C");
}
System.out.print("D");
}
public static void badMethod() {}
}

  1. AC
  2. BC
  3. ACD
  4. ABCD