Online Java Programming Test - Java Programming Test - Random



Loading...  Loading Test...

Instruction:

  • This is a FREE online test. DO NOT pay money to anyone to attend this test.
  • 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 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


2.

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 = "slip";
        String s2 = fix(s1);
        System.out.println(s1 + " " + s2);
    }

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

A.
slip stream
B.
slipstream stream
C.
stream slip stream
D.
slipstream slip stream

Your Answer: Option (Not Answered)

Correct Answer: Option D

Explanation:

When the fix() method is first entered, start()'s s1 and fix()'s s1 reference variables both refer to the same String object (with a value of "slip"). Fix()'s s1 is reassigned to a new object that is created when the concatenation occurs (this second String object has a value of "slipstream"). When the program returns to start(), another String object is created, referred to by s2 and with a value of "stream".

Learn more problems on : Operators and Assignments

Discuss about this problem : Discuss in Forum


3.

What will be the output of the program?

class PassA 
{
    public static void main(String [] args) 
    {
        PassA p = new PassA();
        p.start();
    }

    void start() 
    {
        long [] a1 = {3,4,5};
        long [] a2 = fix(a1);
        System.out.print(a1[0] + a1[1] + a1[2] + " ");
        System.out.println(a2[0] + a2[1] + a2[2]);
    }

    long [] fix(long [] a3) 
    {
        a3[1] = 7;
        return a3;
    }
}

A.
12 15
B.
15 15
C.
3 4 5 3 7 5
D.
3 7 5 3 7 5

Your Answer: Option (Not Answered)

Correct Answer: Option B

Explanation:

Output: 15 15

The reference variables a1 and a3 refer to the same long array object. When the [1] element is updated in the fix() method, it is updating the array referred to by a1. The reference variable a2 refers to the same array object.

So Output: 3+7+5+" "3+7+5

Output: 15 15 Because Numeric values will be added

Learn more problems on : Operators and Assignments

Discuss about this problem : Discuss in Forum


4.

What will be the output of the program?

class A 
{ 
    public A(int x){} 
} 
class B extends A { } 
public class test 
{ 
    public static void main (String args []) 
    {
        A a = new B(); 
        System.out.println("complete"); 
    } 
}

A.
It compiles and runs printing nothing
B.
Compiles but fails at runtime
C.
Compile Error
D.
Prints "complete"

Your Answer: Option (Not Answered)

Correct Answer: Option C

Explanation:

No constructor has been defined for class B therefore it will make a call to the default constructor but since class B extends class A it will also call the Super() default constructor.

Since a constructor has been defined in class A java will no longer supply a default constructor for class A therefore when class B calls class A's default constructor it will result in a compile error.

Learn more problems on : Java.lang Class

Discuss about this problem : Discuss in Forum


5.

Which one is a valid declaration of a boolean?

A.
boolean b1 = 0;
B.
boolean b2 = 'false';
C.
boolean b3 = false;
D.
boolean b4 = Boolean.false();
E.
boolean b5 = no;

Your Answer: Option (Not Answered)

Correct Answer: Option C

Explanation:

A boolean can only be assigned the literal true or false.

Learn more problems on : Language Fundamentals

Discuss about this problem : Discuss in Forum


6.

Which is the valid declarations within an interface definition?

A.
public double methoda();
B.
public final double methoda();
C.
static void methoda(double d1);
D.
protected void methoda(double d1);

Your Answer: Option (Not Answered)

Correct Answer: Option A

Explanation:

Option A is correct. A public access modifier is acceptable. The method prototypes in an interface are all abstract by virtue of their declaration, and should not be declared abstract.

Option B is wrong. The final modifier means that this method cannot be constructed in a subclass. A final method cannot be abstract.

Option C is wrong. static is concerned with the class and not an instance.

Option D is wrong. protected is not permitted when declaring a method of an interface. See information below.

Member declarations in an interface disallow the use of some declaration modifiers; you cannot use transient, volatile, or synchronized in a member declaration in an interface. Also, you may not use the private and protected specifiers when declaring members of an interface.

Learn more problems on : Language Fundamentals

Discuss about this problem : Discuss in Forum


7.

Which one of the following will declare an array and initialize it with five numbers?

A.
Array a = new Array(5);
B.
int [] a = {23,22,21,20,19};
C.
int a [] = new int[5];
D.
int [5] array;

Your Answer: Option (Not Answered)

Correct Answer: Option B

Explanation:

Option B is the legal way to declare and initialize an array with five elements.

Option A is wrong because it shows an example of instantiating a class named Array, passing the integer value 5 to the object's constructor. If you don't see the brackets, you can be certain there is no actual array object! In other words, an Array object (instance of class Array) is not the same as an array object.

Option C is wrong because it shows a legal array declaration, but with no initialization.

Option D is wrong (and will not compile) because it declares an array with a size. Arrays must never be given a size when declared.

Learn more problems on : Language Fundamentals

Discuss about this problem : Discuss in Forum


8.

What two statements are true about the result obtained from calling Math.random()?

  1. The result is less than 0.0.
  2. The result is greater than or equal to 0.0..
  3. The result is less than 1.0.
  4. The result is greater than 1.0.
  5. The result is greater than or equal to 1.0.

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

Your Answer: Option (Not Answered)

Correct Answer: Option B

Explanation:

(2) and (3) are correct. The result range for random() is 0.0 to < 1.0; 1.0 is not in range.

Learn more problems on : Java.lang Class

Discuss about this problem : Discuss in Forum


9.

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?

A.
after line 12
B.
after line 14
C.
after line 7, when doBar() completes
D.
after line 15, when main() completes

Your Answer: Option (Not Answered)

Correct Answer: Option B

Explanation:

Option B is correct. All references to the Bar object created on line 6 are destroyed when a new reference to a new Bar object is assigned to the variable newBar on line 14. Therefore the Bar object, created on line 6, is eligible for garbage collection after line 14.

Option A is wrong. This actually protects the object from garbage collection.

Option C is wrong. Because the reference in the doBar() method is returned on line 7 and is stored in newBar on line 12. This preserver the object created on line 6.

Option D is wrong. Not applicable because the object is eligible for garbage collection after line 14.

Learn more problems on : Garbage Collections

Discuss about this problem : Discuss in Forum


10.

What will be the output of the program (when you run with the -ea option) ?

public class Test 
{  
    public static void main(String[] args) 
    {
        int x = 0;  
        assert (x > 0) : "assertion failed"; /* Line 6 */
        System.out.println("finished"); 
    } 
}

A.
finished
B.
Compilation fails.
C.
An AssertionError is thrown.
D.
An AssertionError is thrown and finished is output.

Your Answer: Option (Not Answered)

Correct Answer: Option C

Explanation:

An assertion Error is thrown as normal giving the output "assertion failed". The word "finished" is not printed (ensure you run with the -ea option)

Assertion failures are generally labeled in the stack trace with the file and line number from which they were thrown, and also in this case with the error's detail message "assertion failed". The detail message is supplied by the assert statement in line 6.

Learn more problems on : Assertions

Discuss about this problem : Discuss in Forum


11.

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

A.
An error at line 11 causes compilation to fail
B.
Errors at lines 8 and 9 cause compilation to fail.
C.
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")
D.
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")

Your Answer: Option (Not Answered)

Correct Answer: Option D

Explanation:

The synchronized code is the key to answering this question. Because x and y are both incremented inside the synchronized method they are always incremented together. Also keep in mind that the two threads share the same reference to the Q126 object.

Also note that because of the infinite loop at line 13, only one thread ever gets to execute.

Learn more problems on : Threads

Discuss about this problem : Discuss in Forum


12.

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

A.
Test( )
B.
Test(void)
C.
public Test( )
D.
public Test(void)

Your Answer: Option (Not Answered)

Correct Answer: Option C

Explanation:

Option A and B are wrong because they use the default access modifier and the access modifier for the class is public (remember, the default constructor has the same access modifier as the class).

Option D is wrong. The void makes the compiler think that this is a method specification - in fact if it were a method specification the compiler would spit it out.

Learn more problems on : Declarations and Access Control

Discuss about this problem : Discuss in Forum


13.

public void foo( boolean a, boolean b)
{ 
    if( a ) 
    {
        System.out.println("A"); /* Line 5 */
    } 
    else if(a && b) /* Line 7 */
    { 
        System.out.println( "A && B"); 
    } 
    else /* Line 11 */
    { 
        if ( !b ) 
        {
            System.out.println( "notB") ;
        } 
        else 
        {
            System.out.println( "ELSE" ) ; 
        } 
    } 
}

A.
If a is true and b is true then the output is "A && B"
B.
If a is true and b is false then the output is "notB"
C.
If a is false and b is true then the output is "ELSE"
D.
If a is false and b is false then the output is "ELSE"

Your Answer: Option (Not Answered)

Correct Answer: Option C

Explanation:

Option C is correct. The output is "ELSE". Only when a is false do the output lines after 11 get some chance of executing.

Option A is wrong. The output is "A". When a is true, irrespective of the value of b, only the line 5 output will be executed. The condition at line 7 will never be evaluated (when a is true it will always be trapped by the line 12 condition) therefore the output will never be "A && B".

Option B is wrong. The output is "A". When a is true, irrespective of the value of b, only the line 5 output will be executed.

Option D is wrong. The output is "notB".

Learn more problems on : Flow Control

Discuss about this problem : Discuss in Forum


14.

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?

A.
Compilation fails.
B.
"odd" will always be output.
C.
"even" will always be output.
D.
"odd" will be output for odd values of x, and "even" for even values.

Your Answer: Option (Not Answered)

Correct Answer: Option A

Explanation:

The compiler will complain because of incompatible types (line 4), the if expects a boolean but it gets an integer.

Learn more problems on : Flow Control

Discuss about this problem : Discuss in Forum


15.

Assume the following method is properly synchronized and called from a thread A on an object B:

wait(2000);

After calling this method, when will the thread A become a candidate to get another turn at the CPU?

A.
After thread A is notified, or after two seconds.
B.
After the lock on B is released, or after two seconds.
C.
Two seconds after thread A is notified.
D.
Two seconds after lock B is released.

Your Answer: Option (Not Answered)

Correct Answer: Option A

Explanation:

Option A. Either of the two events (notification or wait time expiration) will make the thread become a candidate for running again.

Option B is incorrect because a waiting thread will not return to runnable when the lock is released, unless a notification occurs.

Option C is incorrect because the thread will become a candidate immediately after notification, not two seconds afterwards.

Option D is also incorrect because a thread will not come out of a waiting pool just because a lock has been released.

Learn more problems on : Threads

Discuss about this problem : Discuss in Forum


16.

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 an IOException, which of these is true?

A.
This program will compile successfully.
B.
This program fails to compile due to an error at line 4.
C.
This program fails to compile due to an error at line 6.
D.
This program fails to compile due to an error at line 18.

Your Answer: Option (Not Answered)

Correct Answer: Option D

Explanation:

Any method (in this case, the main() method) that throws a checked exception (in this case, out.close() ) must be called within a try clause, or the method must declare that it throws the exception. Either main() must declare that it throws an exception, or the call to out.close() in the finally block must fall inside a (in this case nested) try-catch block.

Learn more problems on : Exceptions

Discuss about this problem : Discuss in Forum


17.

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

A.
i = 6 and j = 5
B.
i = 5 and j = 5
C.
i = 6 and j = 4
D.
i = 5 and j = 6

Your Answer: Option (Not Answered)

Correct Answer: Option D

Explanation:

This loop is a do-while loop, which always executes the code block within the block at least once, due to the testing condition being at the end of the loop, rather than at the beginning. This particular loop is exited prematurely if i becomes greater than j.

The order is, test i against j, if bigger, it breaks from the loop, decrements j by one, and then tests the loop condition, where a pre-incremented by one i is tested for being lower than 5. The test is at the end of the loop, so i can reach the value of 5 before it fails. So it goes, start:

1, 10

2, 9

3, 8

4, 7

5, 6 loop condition fails.

Learn more problems on : Flow Control

Discuss about this problem : Discuss in Forum


18.

Which statement is true for the class java.util.ArrayList?

A.
The elements in the collection are ordered.
B.
The collection is guaranteed to be immutable.
C.
The elements in the collection are guaranteed to be unique.
D.
The elements in the collection are accessed using a unique key.

Your Answer: Option (Not Answered)

Correct Answer: Option A

Explanation:

Yes, always the elements in the collection are ordered.

Learn more problems on : Objects and Collections

Discuss about this problem : Discuss in Forum


19.

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


20.

What will be the output of the program?

public class Test 
{  
    public static void aMethod() throws Exception 
    {
        try /* Line 5 */
        {
            throw new Exception(); /* Line 7 */
        } 
        finally /* Line 9 */
        {
            System.out.print("finally "); /* Line 11 */
        } 
    } 
    public static void main(String args[]) 
    {
        try 
        {
            aMethod();  
        } 
        catch (Exception e) /* Line 20 */
        {
            System.out.print("exception "); 
        } 
        System.out.print("finished"); /* Line 24 */
    } 
}

A.
finally
B.
exception finished
C.
finally exception finished
D.
Compilation fails

Your Answer: Option (Not Answered)

Correct Answer: Option C

Explanation:

This is what happens:

(1) The execution of the try block (line 5) completes abruptly because of the throw statement (line 7).

(2) The exception cannot be assigned to the parameter of any catch clause of the try statement therefore the finally block is executed (line 9) and "finally" is output (line 11).

(3) The finally block completes normally, and then the try statement completes abruptly because of the throw statement (line 7).

(4) The exception is propagated up the call stack and is caught by the catch in the main method (line 20). This prints "exception".

(5) Lastly program execution continues, because the exception has been caught, and "finished" is output (line 24).

Learn more problems on : Exceptions

Discuss about this problem : Discuss in Forum


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