Online Java Programming Test - Java Programming Test 9



Loading...  Loading Test...

Instruction:

  • Total number of questions : 20.
  • Time alloted : 30 minutes.
  • Each question carry 1 mark, no negative marks.
  • DO NOT refresh the page.
  • All the best :-).

1.

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); 
    } 
}

A.
0
B.
1
C.
2
D.
Compilation fails.

Your Answer: Option (Not Answered)

Correct Answer: Option D

Explanation:

A default no-args constructor is not created because there is a constructor supplied that has an argument, line 4. Therefore the sub-class constructor must explicitly make a call to the super class constructor:

public Sub(String text)
{ 
    super(text); // this must be the first line constructor 
    i = 2; 
}

Learn more problems on : Declarations and Access Control

Discuss about this problem : Discuss in Forum

2.

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 */
    } 
}

A.
Base
B.
BaseBase
C.
Compilation fails
D.
The code runs with no output

Your Answer: Option (Not Answered)

Correct Answer: Option B

Explanation:

Option B is correct. It would be correct if the code had compiled, and the subclass Alpha had 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 causes Base 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.

Learn more problems on : Declarations and Access Control

Discuss about this problem : Discuss in Forum

3.

What will be the output of the program?

import java.util.*;
public class NewTreeSet2 extends NewTreeSet 
{
    public static void main(String [] args) 
    {
        NewTreeSet2 t = new NewTreeSet2();
        t.count();
    }
}
protected class NewTreeSet
{
    void count() 
    {
        for (int x = 0; x < 7; x++,x++ ) 
        {
            System.out.print(" " + x);
        }
    }
}

A.
0 2 4
B.
0 2 4 6
C.
Compilation fails at line 2
D.
Compilation fails at line 10

Your Answer: Option (Not Answered)

Correct Answer: Option D

Explanation:

Nonnested classes cannot be marked protected (or final for that matter), so the compiler will fail at protected class NewTreeSet.

Learn more problems on : Declarations and Access Control

Discuss about this problem : Discuss in Forum

4.

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 called run(), and cannot take any arguments.

A.
1 and 3
B.
2 and 4
C.
1 and 5
D.
2 and 6

Your Answer: Option (Not Answered)

Correct Answer: Option D

Explanation:

(2) and (6). When a thread's run() method completes, the thread will die. The run() method must be declared public void and not take any arguments.

(1) is incorrect because classes can never extend interfaces. (3) is incorrect because the run() method is typically not empty; if it were, the thread would do nothing. (4) is incorrect because the mandatory method is run(). (5) is incorrect because the class implements Runnable.

Learn more problems on : Declarations and Access Control

Discuss about this problem : Discuss in Forum

5.

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 22 */
    } 
}

A.
ABCD
B.
Compilation fails.
C.
C is printed before exiting with an error message.
D.
BC is printed before exiting with an error message.

Your Answer: Option (Not Answered)

Correct Answer: Option C

Explanation:

Error is thrown but not recognised line(22) because the only catch attempts to catch an Exception and Exception is not a superclass of Error. Therefore only the code in the finally statement can be run before exiting with a runtime error (Exception in thread "main" java.lang.Error).

Learn more problems on : Exceptions

Discuss about this problem : Discuss in Forum

6.

What will be the output of the program?

public class X 
{  
    public static void main(String [] args) 
    {
        try 
        {
            badMethod(); /* Line 7 */
            System.out.print("A"); 
        } 
        catch (Exception ex) /* Line 10 */
        {
            System.out.print("B"); /* Line 12 */
        } 
        finally /* Line 14 */
        {
            System.out.print("C"); /* Line 16 */
        }  
        System.out.print("D"); /* Line 18 */
    } 
    public static void badMethod() 
    {
        throw new RuntimeException(); 
    } 
}

A.
AB
B.
BC
C.
ABC
D.
BCD

Your Answer: Option (Not Answered)

Correct Answer: Option D

Explanation:

(1) A RuntimeException is thrown, this is a subclass of exception.

(2) The exception causes the try to complete abruptly (line 7) therefore line 8 is never executed.

(3) The exception is caught (line 10) and "B" is output (line 12)

(4) The finally block (line 14) is always executed and "C" is output (line 16).

(5) The exception was caught, so the program continues with line 18 and outputs "D".

Learn more problems on : Exceptions

Discuss about this problem : Discuss in Forum

7.

Which interface does java.util.Hashtable implement?

A.
Java.util.Map
B.
Java.util.List
C.
Java.util.HashTable
D.
Java.util.Collection

Your Answer: Option (Not Answered)

Correct Answer: Option A

Explanation:

Hash table based implementation of the Map interface.

Learn more problems on : Objects and Collections

Discuss about this problem : Discuss in Forum

8.

What will be the output of the program?

package foo; 
import java.util.Vector; /* Line 2 */
private class MyVector extends Vector 
{
    int i = 1; /* Line 5 */
    public MyVector() 
    { 
        i = 2; 
    } 
} 
public class MyNewVector extends MyVector 
{
    public MyNewVector () 
    { 
        i = 4; /* Line 15 */
    } 
    public static void main (String args []) 
    { 
        MyVector v = new MyNewVector(); /* Line 19 */
    } 
}

A.
Compilation will succeed.
B.
Compilation will fail at line 3.
C.
Compilation will fail at line 5.
D.
Compilation will fail at line 15.

Your Answer: Option (Not Answered)

Correct Answer: Option B

Explanation:

Option B is correct. The compiler complains with the error "modifier private not allowed here". The class is created private and is being used by another class on line 19.

Learn more problems on : Objects and Collections

Discuss about this problem : Discuss in Forum

9.

What two statements are true about properly overridden hashCode() and equals() methods?

  1. hashCode() doesn't have to be overridden if equals() is.
  2. equals() doesn't have to be overridden if hashCode() is.
  3. hashCode() can always return the same value, regardless of the object that invoked it.
  4. equals() can be true even if it's comparing different objects.

A.
1 and 2
B.
2 and 3
C.
3 and 4
D.
1 and 3

Your Answer: Option (Not Answered)

Correct Answer: Option C

Explanation:

(3) and (4) are correct.

(1) and (2) are incorrect because by contract hashCode() and equals() can't be overridden unless both are overridden.

Learn more problems on : Objects and Collections

Discuss about this problem : Discuss in Forum

10.

Which three are methods of the Object class?

  1. notify();
  2. notifyAll();
  3. isInterrupted();
  4. synchronized();
  5. interrupt();
  6. wait(long msecs);
  7. sleep(long msecs);
  8. yield();

A.
1, 2, 4
B.
2, 4, 5
C.
1, 2, 6
D.
2, 3, 4

Your Answer: Option (Not Answered)

Correct Answer: Option C

Explanation:

(1), (2), and (6) are correct. They are all related to the list of threads waiting on the specified object.

(3), (5), (7), and (8) are incorrect answers. The methods isInterrupted() and interrupt() are instance methods of Thread.

The methods sleep() and yield() are static methods of Thread.

D is incorrect because synchronized is a keyword and the synchronized() construct is part of the Java language.

Learn more problems on : Threads

Discuss about this problem : Discuss in Forum

11.

Which cannot directly cause a thread to stop executing?

A.
Calling the SetPriority() method on a Thread object.
B.
Calling the wait() method on an object.
C.
Calling notify() method on an object.
D.
Calling read() method on an InputStream object.

Your Answer: Option (Not Answered)

Correct Answer: Option C

Explanation:

Option C is correct. notify() - wakes up a single thread that is waiting on this object's monitor.

Learn more problems on : Threads

Discuss about this problem : Discuss in Forum

12.

Which method registers a thread in a thread scheduler?

A.
run();
B.
construct();
C.
start();
D.
register();

Your Answer: Option (Not Answered)

Correct Answer: Option C

Explanation:

Option C is correct. The start() method causes this thread to begin execution; the Java Virtual Machine calls the run method of this thread.

Option A is wrong. The run() method of a thread is like the main() method to an application. Starting the thread causes the object's run method to be called in that separately executing thread.

Option B is wrong. There is no construct() method in the Thread class.

Option D is wrong. There is no register() method in the Thread class.

Learn more problems on : Threads

Discuss about this problem : Discuss in Forum

13.

public class MyRunnable implements Runnable 
{
    public void run() 
    {
        // some code here
    }
}
which of these will create and start this thread?

A.
new Runnable(MyRunnable).start();
B.
new Thread(MyRunnable).run();
C.
new Thread(new MyRunnable()).start();
D.
new MyRunnable().start();

Your Answer: Option (Not Answered)

Correct Answer: Option C

Explanation:

Because the class implements Runnable, an instance of it has to be passed to the Thread constructor, and then the instance of the Thread has to be started.

A is incorrect. There is no constructor like this for Runnable because Runnable is an interface, and it is illegal to pass a class or interface name to any constructor.

B is incorrect for the same reason; you can't pass a class or interface name to any constructor.

D is incorrect because MyRunnable doesn't have a start() method, and the only start() method that can start a thread of execution is the start() in the Thread class.

Learn more problems on : Threads

Discuss about this problem : Discuss in Forum

14.

void start() {  
    A a = new A(); 
    B b = new B(); 
    a.s(b);  
    b = null; /* Line 5 */
    a = null;  /* Line 6 */
    System.out.println("start completed"); /* Line 7 */
} 
When is the B object, created in line 3, eligible for garbage collection?

A.
after line 5
B.
after line 6
C.
after line 7
D.
There is no way to be absolutely certain.

Your Answer: Option (Not Answered)

Correct Answer: Option D

Learn more problems on : Garbage Collections

Discuss about this problem : Discuss in Forum

15.

class X2 
{
    public X2 x;
    public static void main(String [] args) 
    {
        X2 x2 = new X2();  /* Line 6 */
        X2 x3 = new X2();  /* Line 7 */
        x2.x = x3;
        x3.x = x2;
        x2 = new X2();
        x3 = x2; /* Line 11 */
        doComplexStuff();
    }
}
after line 11 runs, how many objects are eligible for garbage collection?

A.
0
B.
1
C.
2
D.
3

Your Answer: Option (Not Answered)

Correct Answer: Option C

Explanation:

This is an example of the islands of isolated objects. By the time line 11 has run, the objects instantiated in lines 6 and 7 are referring to each other, but no live thread can reach either of them.

Learn more problems on : Garbage Collections

Discuss about this problem : Discuss in Forum

16.

Which of the following would compile without error?

A.
int a = Math.abs(-5);
B.
int b = Math.abs(5.0);
C.
int c = Math.abs(5.5F);
D.
int d = Math.abs(5L);

Your Answer: Option (Not Answered)

Correct Answer: Option A

Explanation:

The return value of the Math.abs() method is always the same as the type of the parameter passed into that method.

In the case of A, an integer is passed in and so the result is also an integer which is fine for assignment to "int a".

The values used in B, C & D respectively are a double, a float and a long. The compiler will complain about a possible loss of precision if we try to assign the results to an "int".

Learn more problems on : Java.lang Class

Discuss about this problem : Discuss in Forum

17.

What will be the output of the program?

String x = new String("xyz");
String y = "abc";
x = x + y;
How many String objects have been created?

A.
2
B.
3
C.
4
D.
5

Your Answer: Option (Not Answered)

Correct Answer: Option C

Explanation:

Line 1 creates two, one referred to by x and the lost String "xyz". Line 2 creates one (for a total of three). Line 3 creates one more (for a total of four), the concatenated String referred to by x with a value of "xyzabc".

Learn more problems on : Java.lang Class

Discuss about this problem : Discuss in Forum

18.

What will be the output of the program?

System.out.println(Math.sqrt(-4D));

A.
-2
B.
NaN
C.
Compile Error
D.
Runtime Exception

Your Answer: Option (Not Answered)

Correct Answer: Option B

Explanation:

It is not possible in regular mathematics to get a value for the square-root of a negative number therefore a NaN will be returned because the code is valid.

Learn more problems on : Java.lang Class

Discuss about this problem : Discuss in Forum

19.

What will be the output of the program?

public class Test 
{ 
    public static void main(String[] args) 
    {
        final StringBuffer a = new StringBuffer(); 
        final StringBuffer b = new StringBuffer(); 

        new Thread() 
        { 
            public void run() 
            {
                System.out.print(a.append("A")); 
                synchronized(b) 
                { 
                    System.out.print(b.append("B")); 
                } 
            } 
        }.start(); 
            
        new Thread() 
        {
            public void run() 
            {
                System.out.print(b.append("C")); 
                synchronized(a) 
                {
                    System.out.print(a.append("D")); 
                } 
            } 
        }.start(); 
    } 
}

A.
ACCBAD
B.
ABBCAD
C.
CDDACB
D.
Indeterminate output

Your Answer: Option (Not Answered)

Correct Answer: Option D

Explanation:

It gives different output while executing the same compiled code at different times.

C:\>javac Test.java
C:\>java Test
ABBCAD
C:\>java Test
ACADCB
C:\>java Test
ACBCBAD
C:\>java Test
ABBCAD
C:\>java Test
ACBCBAD
C:\>java Test
ACBCBAD
C:\>java Test
ABBCAD

Learn more problems on : Java.lang Class

Discuss about this problem : Discuss in Forum

20.

Which statement is true given the following?

Double d = Math.random();

A.
0.0 < d <= 1.0
B.
0.0 <= d < 1.0
C.
Compilation fail
D.
Cannot say.

Your Answer: Option (Not Answered)

Correct Answer: Option B

Explanation:

The Math.random() method returns a double value with a positive sign, greater than or equal to 0.0 and less than 1.0

Learn more problems on : Java.lang Class

Discuss about this problem : Discuss in Forum

Submit your test now to view the Results and Statistics with answer explanation.