Java Programming Fundamentals Practice Test

Test your knowledge of core Java programming concepts including classes, interfaces, inheritance, threading, exception handling, and object-oriented programming principles.

25 Questions Published

Questions

Question 1 Multiple Choice (Single Answer)

What will be the output of the program?

class Super
{ 
    public int i = 0; 

    public Super(String text) /* Line 4 */
    {
        i = 1; 
    } 
} 

class Sub extends Super
{
    public Sub(String text)
    {
        i = 2; 
    } 

    public static void main(String args[])
    {
        Sub sub = new Sub(Hello); 
        System.out.println(sub.i); 
    } 
}
  1. 0
  2. 1
  3. 2
  4. Compilation fails
Question 2 Multiple Choice (Single Answer)

What will be the output of the program?

public abstract class AbstractTest {
 public int getNum() {
  return 45;
 }
 public abstract class Bar {
  public int getNum() {
   return 38;
  }
 }
 public static void main(String[] args) {
  AbstractTest t = new AbstractTest() {
   public int getNum() {
    return 22;
   }
  };
  AbstractTest.Bar f = t.new Bar() {
   public int getNum() {
    return 57;
   }
  };
  System.out.println(f.getNum() + +t.getNum());
 }
}
  1. 57 22
  2. 45 38
  3. 45 57
  4. An exception occurs at runtime
Question 3 Multiple Choice (Single Answer)

Which of the following statements is true?

  1. It is sometimes good practice to throw an AssertionError explicitly.
  2. Private getter() and setter() methods should not use assertions to verify arguments.
  3. If an AssertionError is thrown in a try-catch block, the finally block will be bypassed.
  4. It is proper to handle assertion statement failures using a catch (AssertionException ae) block.
Question 4 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() {
  throw new Error(); /* Line 18 */
 }
}
  1. ABCD.
  2. Compilation fails.
  3. C is printed before exiting with an error message.
  4. BC is printed before exiting with an error message.
Question 5 Multiple Choice (Single Answer)

What will be the output of the program?

public class ThreadTest extends Thread 
{ 
    public void run() 
    { 
        System.out.println("In run"); 
        yield(); 
        System.out.println("Leaving run"); 
    } 
    public static void main(String []argv) 
    { 
        (new ThreadTest()).start(); 
    } 
}
  1. The code fails to compile in the main() method
  2. The code fails to compile in the run() method
  3. Only the text "In run" will be displayed
  4. The text "In run" followed by "Leaving run" will be displayed
Question 6 Multiple Choice (Single Answer)

What will be the output of the program?

public class Test107 implements Runnable 
{ 
    private int x; 
    private int y; 

    public static void main(String args[]) 
    {
        Test107 that = new Test107(); 
        (new Thread(that)).start(); 
        (new Thread(that)).start(); 
    } 
    public synchronized void run() 
    {
        for(int i = 0; i < 10; i++) 
        { 
            x++; 
            y++; 
            System.out.println(x =  + x + , y =  + y); /* Line 17 */
        } 
    } 
}
  1. Compilation error.
  2. Will print in this order: x = 1 y = 1 x = 2 y = 2 x = 3 y = 3 x = 4 y = 4 x = 5 y = 5... but the output will be produced by both threads running simultaneously.
  3. Will print in this order: x = 1 y = 1 x = 2 y = 2 x = 3 y = 3 x = 4 y = 4 x = 5 y = 5... but the output will be produced by first one thread then the other. This is guaranteed by the synchronised code.
  4. Will print in this order x = 1 y = 2 x = 3 y = 4 x = 5 y = 6 x = 7 y = 8...
Question 7 Multiple Choice (Single Answer)

x = 0;
if (x1.hashCode() != x2.hashCode() ) x = x + 1;
if (x3.equals(x4) ) x = x + 10;
if (!x5.equals(x6) ) x = x + 100;
if (x7.hashCode() == x8.hashCode() ) x = x + 1000;
System.out.println("x = " + x);
and assuming that the equals() and hashCode() methods are property implemented, if the output is "x = 1111", which of the following statements will always be true?

  1. x2.equals(x1)
  2. x3.hashCode() == x4.hashCode()
  3. x5.hashCode() != x6.hashCode()
  4. x8.equals(x7)
Question 8 Multiple Choice (Single Answer)

public class While
{
public void loop()
{
int x= 0;
while ( 1 ) /* Line 6 /
{
System.out.print("x plus one is " + (x + 1)); /
Line 8 */
}
}
}
Which statement is true?

  1. There is a syntax error on line 1.
  2. There are syntax errors on lines 1 and 6.
  3. There are syntax errors on lines 1, 6, and 8.
  4. There is a syntax error on line 6.
Question 9 Multiple Choice (Single Answer)

What will be the output of the program?
int I = 0;
outer:
while (true)
{
I++;
inner:
for (int j = 0; j < 10; j++)
{
I += j;
if (j == 3)
continue inner;
break outer;
}
continue outer;
}
System.out.println(I);

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

Which two of the following are legal declarations for nonnested classes and interfaces?

  1. final abstract class Test {}
  2. public static interface Test {}
  3. final public class Test {}
  4. protected abstract class Test {}
  5. protected interface Test {}
  6. abstract public class Test {}
  1. 1 and 4
  2. 2 and 5
  3. 3 and 6
  4. 4 and 6
Question 11 Multiple Choice (Single Answer)

Which statement is true about a static nested class?

  1. You must have a reference to an instance of the enclosing class in order to instantiate it.
  2. It does not have access to nonstatic members of the enclosing class.
  3. It's variables and methods must be static.
  4. It must extend the enclosing class.
Question 12 Multiple Choice (Single Answer)

What will be the output of the program?
public class Test
{
private static float[] f = new float[2];
public static void main (String[] args)
{
System.out.println("f[0] = " + f[0]);
}
}

  1. f[0] = 0
  2. f[0] = 0.0
  3. Compile Error
  4. Runtime Exception
Question 13 Multiple Choice (Single Answer)

Which two statements are true about comparing two instances of the same class, given that

  1. the equals() and hashCode() methods have been properly overridden?
  2. If the equals() method returns true, the hashCode() comparison == must return true.
  3. If the equals() method returns false, the hashCode() comparison != must return true.
  4. If the hashCode() comparison == returns true, the equals() method must return true.
  5. If the hashCode() comparison == returns true, the equals() method might return true.
  1. 1 and 4
  2. 2 and 3
  3. 3 and 4
  4. 1 and 3
Question 14 Multiple Choice (Single Answer)

Which constructs an anonymous inner class instance?

  1. Runnable r = new Runnable() { };
  2. Runnable r = new Runnable(public void run() { });
  3. Runnable r = new Runnable { public void run(){}};
  4. System.out.println(new Runnable() {public void run() { }});
Question 15 Multiple Choice (Single Answer)

class Boo
{
Boo(String s) { }
Boo() { }
}
class Bar extends Boo
{
Bar() { }
Bar(String s) {super(s);}
void zoo()
{
// insert code here
}
}
which one create an anonymous inner class from within class Bar?

  1. Boo f = new Boo(24) { };
  2. Boo f = new Bar() { };
  3. Bar f = new Boo(String s) { };
  4. Boo f = new Boo.Bar(String s) { };
Question 16 Multiple Choice (Single Answer)

What will be the output of the program?
class MyThread extends Thread
{
public static void main(String [] args)
{
MyThread t = new MyThread(); /* Line 5 /
t.run(); /
Line 6 */
}

public void run() 
{
    for(int i=1; i &lt; 3; ++i) 
    {
        System.out.print(i + ..);
    }
}

}

  1. This code will not compile due to line 5.
  2. This code will not compile due to line 6.
  3. 1..2..
  4. 1..2..3..
Question 17 Multiple Choice (Single Answer)

What will be the output of the program?
public class HorseTest
{
public static void main (String [] args)
{
class Horse
{
public String name; /* Line 7 /
public Horse(String s)
{
name = s;
}
} /
class Horse ends */

    Object obj = new Horse(&quot;Zippo&quot;); /* Line 13 */
    Horse h = (Horse) obj; /* Line 14 */
    System.out.println(h.name);
}

} /* class HorseTest ends */

  1. An exception occurs at runtime at line 10.
  2. It prints &quot;Zippo&quot;.
  3. Compilation fails because of an error on line 7.
  4. Compilation fails because of an error on line 13.
Question 18 Multiple Choice (Single Answer)

What allows the programmer to destroy an object x?

  1. x.delete()
  2. x.finalize()
  3. Runtime.getRuntime().gc()
  4. Only the garbage collection system can destroy an object.
Question 19 Multiple Choice (Single Answer)

What will be the output of the program?
public class Q126 implements Runnable
{
private int x;
private int y;

public static void main(String [] args) 
{ 
    Q126 that = new Q126(); 
    (new Thread(that)).start( ); /* Line 8 */
    (new Thread(that)).start( ); /* Line 9 */
} 
public synchronized void run( ) /* Line 11 */
{ 
    for (;;) /* Line 13 */
    { 
        x++; 
        y++; 
        System.out.println(&quot;x = &quot; + x + &quot;y = &quot; + y); 
    } 
} 

}

  1. An error at line 11 causes compilation to fail
  2. Errors at lines 8 and 9 cause compilation to fail.
  3. The program prints pairs of values for x and y that might not always be the same on the same line (for example, &quot;x=2, y=1&quot;)
  4. The program prints pairs of values for x and y that are always the same on the same line (for example, &quot;x=1, y=1&quot;. In addition, each value appears once (for example, &quot;x=1, y=1&quot; followed by &quot;x=2, y=2&quot;)
Question 20 Multiple Choice (Single Answer)

Option B is correct. It would be correct if the code had compiled, and the subclass Alphahad been saved in its own file. In this case Java supplies an implicit call from the sub-class constructor to the no-args constructor of the super-class therefore line 12 causesBase to be output. Line 13 also causes Base to be output.
Option A is wrong. It would be correct if either the main class or the subclass had not been instantiated.
Option C is wrong. The code compiles.
Option D is wrong. There is output.

  1. 0 2 4
  2. 0 2 4 6
  3. Compilation fails at line 2
  4. Compilation fails at line 10
Question 21 Multiple Choice (Single Answer)

What will be the output of the program?
class Q207
{
public static void main(String[] args)
{
int i1 = 5;
int i2 = 6;
String s1 = "7";
System.out.println(i1 + i2 + s1); /* Line 8 */
}
}

  1. 18
  2. 117
  3. 567
  4. Compiler error
Question 22 Multiple Choice (Single Answer)

What will be the output of the program?
public class StringRef
{
public static void main(String [] args)
{
String s1 = "abc";
String s2 = "def";
String s3 = s2; /* Line 7 */
s2 = "ghi";
System.out.println(s1 + s2 + s3);
}
}

  1. abcdefghi
  2. abcdefdef
  3. abcghidef
  4. abcghighi
Question 23 Multiple Choice (Single Answer)

What will be the output of the program?
class Base
{
Base()
{
System.out.print("Base");
}
}
public class Alpha extends Base
{
public static void main(String[] args)
{
new Alpha(); /* Line 12 /
new Base(); /
Line 13 */
}
}

  1. Base
  2. BaseBase
  3. Compilation fails
  4. The code runs with no output
Question 24 Multiple Choice (Single Answer)

What will be the output of the program?
public class WrapTest
{
public static void main(String [] args)
{
int result = 0;
short s = 42;
Long x = new Long("42");
Long y = new Long(42);
Short z = new Short("42");
Short x2 = new Short(s);
Integer y2 = new Integer("42");
Integer z2 = new Integer(42);

    if (x == y) /* Line 13 */
        result = 1;
    if (x.equals(y) ) /* Line 15 */
        result = result + 10;
    if (x.equals(z) ) /* Line 17 */
        result = result + 100;
    if (x.equals(x2) ) /* Line 19 */
        result = result + 1000;
    if (x.equals(z2) ) /* Line 21 */
        result = result + 10000;

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

}

  1. result = 1
  2. result = 10
  3. result = 11
  4. result = 11010
Question 25 Multiple Choice (Single Answer)

Which two statements are true for any concrete class implementing the java.lang.Runnable interface?

  1. You can extend the Runnable interface as long as you override the public run()method.
  2. The class must contain a method called run() from which all code for that thread will be initiated.
  3. The class must contain an empty public void method named run().
  4. The class must contain a public void method named runnable().
  5. The class definition must include the words implements Threads and contain a method called run().
  6. The mandatory method must be public, with a return type of void, must be calledrun(), and cannot take any arguments.
  1. 1 and 3
  2. 2 and 4
  3. 1 and 5
  4. 2 and 6