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.
Questions
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);
}
}
- 0
- 1
- 2
- Compilation fails
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());
}
}
- 57 22
- 45 38
- 45 57
- An exception occurs at runtime
Which of the following statements is true?
- It is sometimes good practice to throw an AssertionError explicitly.
- Private getter() and setter() methods should not use assertions to verify arguments.
- If an AssertionError is thrown in a try-catch block, the finally block will be bypassed.
- It is proper to handle assertion statement failures using a catch (AssertionException ae) block.
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 */
}
}
- ABCD.
- Compilation fails.
- C is printed before exiting with an error message.
- BC is printed before exiting with an error message.
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();
}
}
- The code fails to compile in the main() method
- The code fails to compile in the run() method
- Only the text "In run" will be displayed
- The text "In run" followed by "Leaving run" will be displayed
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 */
}
}
}
- Compilation error.
- 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.
- 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.
- Will print in this order x = 1 y = 2 x = 3 y = 4 x = 5 y = 6 x = 7 y = 8...
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?
- x2.equals(x1)
- x3.hashCode() == x4.hashCode()
- x5.hashCode() != x6.hashCode()
- x8.equals(x7)
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?
- There is a syntax error on line 1.
- There are syntax errors on lines 1 and 6.
- There are syntax errors on lines 1, 6, and 8.
- There is a syntax error on line 6.
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
- 2
- 3
- 4
Which two of the following are legal declarations for nonnested classes and interfaces?
- final abstract class Test {}
- public static interface Test {}
- final public class Test {}
- protected abstract class Test {}
- protected interface Test {}
- abstract public class Test {}
- 1 and 4
- 2 and 5
- 3 and 6
- 4 and 6
Which statement is true about a static nested class?
- You must have a reference to an instance of the enclosing class in order to instantiate it.
- It does not have access to nonstatic members of the enclosing class.
- It's variables and methods must be static.
- It must extend the enclosing class.
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]);
}
}
- f[0] = 0
- f[0] = 0.0
- Compile Error
- Runtime Exception
Which two statements are true about comparing two instances of the same class, given that
- the equals() and hashCode() methods have been properly overridden?
- If the equals() method returns true, the hashCode() comparison == must return true.
- If the equals() method returns false, the hashCode() comparison != must return true.
- If the hashCode() comparison == returns true, the equals() method must return true.
- If the hashCode() comparison == returns true, the equals() method might return true.
- 1 and 4
- 2 and 3
- 3 and 4
- 1 and 3
Which constructs an anonymous inner class instance?
- Runnable r = new Runnable() { };
- Runnable r = new Runnable(public void run() { });
- Runnable r = new Runnable { public void run(){}};
- System.out.println(new Runnable() {public void run() { }});
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?
- Boo f = new Boo(24) { };
- Boo f = new Bar() { };
- Bar f = new Boo(String s) { };
- Boo f = new Boo.Bar(String s) { };
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 < 3; ++i)
{
System.out.print(i + ..);
}
}
}
- This code will not compile due to line 5.
- This code will not compile due to line 6.
- 1..2..
- 1..2..3..
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("Zippo"); /* Line 13 */
Horse h = (Horse) obj; /* Line 14 */
System.out.println(h.name);
}
} /* class HorseTest ends */
- An exception occurs at runtime at line 10.
- It prints "Zippo".
- Compilation fails because of an error on line 7.
- Compilation fails because of an error on line 13.
What allows the programmer to destroy an object x?
- x.delete()
- x.finalize()
- Runtime.getRuntime().gc()
- Only the garbage collection system can destroy an object.
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("x = " + x + "y = " + y);
}
}
}
- An error at line 11 causes compilation to fail
- Errors at lines 8 and 9 cause compilation to fail.
- The program prints pairs of values for x and y that might not always be the same on the same line (for example, "x=2, y=1")
- The program prints pairs of values for x and y that are always the same on the same line (for example, "x=1, y=1". In addition, each value appears once (for example, "x=1, y=1" followed by "x=2, y=2")
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.
- 0 2 4
- 0 2 4 6
- Compilation fails at line 2
- Compilation fails at line 10
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 */
}
}
- 18
- 117
- 567
- Compiler error
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);
}
}
- abcdefghi
- abcdefdef
- abcghidef
- abcghighi
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 */
}
}
- Base
- BaseBase
- Compilation fails
- The code runs with no output
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("result = " + result);
}
}
- result = 1
- result = 10
- result = 11
- result = 11010
Which two statements are true for any concrete class implementing the java.lang.Runnable interface?
- You can extend the Runnable interface as long as you override the public run()method.
- The class must contain a method called run() from which all code for that thread will be initiated.
- The class must contain an empty public void method named run().
- The class must contain a public void method named runnable().
- The class definition must include the words implements Threads and contain a method called run().
- The mandatory method must be public, with a return type of void, must be calledrun(), and cannot take any arguments.
- 1 and 3
- 2 and 4
- 1 and 5
- 2 and 6