Online Java Programming Test - Java Programming Test - Random



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?

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

2.

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

3.

switch(x) 
{ 
    default:  
        System.out.println("Hello"); 
}
Which two are acceptable types for x?
  1. byte
  2. long
  3. char
  4. float
  5. Short
  6. Long

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

Your Answer: Option (Not Answered)

Correct Answer: Option A

Explanation:

Switch statements are based on integer expressions and since both bytes and chars can implicitly be widened to an integer, these can also be used. Also shorts can be used. Short and Long are wrapper classes and reference types can not be used as variables.

Learn more problems on : Flow Control

Discuss about this problem : Discuss in Forum

4.

Which collection class allows you to associate its elements with key values, and allows you to retrieve objects in FIFO (first-in, first-out) sequence?

A.
java.util.ArrayList
B.
java.util.LinkedHashMap
C.
java.util.HashMap
D.
java.util.TreeMap

Your Answer: Option (Not Answered)

Correct Answer: Option B

Explanation:

LinkedHashMap is the collection class used for caching purposes. FIFO is another way to indicate caching behavior. To retrieve LinkedHashMap elements in cached order, use the values() method and iterate over the resultant collection.

Learn more problems on : Objects and Collections

Discuss about this problem : Discuss in Forum

5.

Suppose that you would like to create an instance of a new Map that has an iteration order that is the same as the iteration order of an existing instance of a Map. Which concrete implementation of the Map interface should be used for the new instance?

A.
TreeMap
B.
HashMap
C.
LinkedHashMap
D.
The answer depends on the implementation of the existing instance.

Your Answer: Option (Not Answered)

Correct Answer: Option C

Explanation:

The iteration order of a Collection is the order in which an iterator moves through the elements of the Collection. The iteration order of a LinkedHashMap is determined by the order in which elements are inserted.

When a new LinkedHashMap is created by passing a reference to an existing Collection to the constructor of a LinkedHashMap the Collection.addAll method will ultimately be invoked.

The addAll method uses an iterator to the existing Collection to iterate through the elements of the existing Collection and add each to the instance of the new LinkedHashMap.

Since the iteration order of the LinkedHashMap is determined by the order of insertion, the iteration order of the new LinkedHashMap must be the same as the interation order of the old Collection.

Learn more problems on : Objects and Collections

Discuss about this problem : Discuss in Forum

6.

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

7.

Which two statements are true about wrapper or String classes?

  1. If x and y refer to instances of different wrapper classes, then the fragment x.equals(y) will cause a compiler failure.
  2. If x and y refer to instances of different wrapper classes, then x == y can sometimes be true.
  3. If x and y are String references and if x.equals(y) is true, then x == y is true.
  4. If x, y, and z refer to instances of wrapper classes and x.equals(y) is true, and y.equals(z) is true, then z.equals(x) will always be true.
  5. If x and y are String references and x == y is true, then y.equals(x) will be true.

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

Your Answer: Option (Not Answered)

Correct Answer: Option D

Explanation:

Statement (4) describes an example of the equals() method behaving transitively. By the way, x, y,and z will all be the same type of wrapper. Statement (5) is true because x and y are referring to the same String object.

Statement (1) is incorrect—the fragment will compile. Statement (2) is incorrect because x == y means that the two reference variables are referring to the same object. Statement (3) will only be true if x and y refer to the same String. It is possible for x and y to refer to two different String objects with the same value.

Learn more problems on : Java.lang Class

Discuss about this problem : Discuss in Forum

8.

Which of the following are valid calls to Math.max?

  1. Math.max(1,4)
  2. Math.max(2.3, 5)
  3. Math.max(1, 3, 5, 7)
  4. Math.max(-1.5, -2.8f)

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

Your Answer: Option (Not Answered)

Correct Answer: Option A

Explanation:

(1), (2), and (4) are correct. The max() method is overloaded to take two arguments of type int, long, float, or double.

(3) is incorrect because the max() method only takes two arguments.

Learn more problems on : Java.lang Class

Discuss about this problem : Discuss in Forum

9.

What will be the output of the program?

public class Foo 
{  
    public static void main(String[] args) 
    {
        try 
        { 
            return; 
        } 
        finally 
        {
            System.out.println( "Finally" ); 
        } 
    } 
}

A.
Finally
B.
Compilation fails.
C.
The code runs with no output.
D.
An exception is thrown at runtime.

Your Answer: Option (Not Answered)

Correct Answer: Option A

Explanation:

If you put a finally block after a try and its associated catch blocks, then once execution enters the try block, the code in that finally block will definitely be executed except in the following circumstances:
  1. An exception arising in the finally block itself.
  2. The death of the thread.
  3. The use of System.exit()
  4. Turning off the power to the CPU.

I suppose the last three could be classified as VM shutdown.

Learn more problems on : Exceptions

Discuss about this problem : Discuss in Forum

10.

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

11.

Which of the following statements is true?

A.
It is sometimes good practice to throw an AssertionError explicitly.
B.
Private getter() and setter() methods should not use assertions to verify arguments.
C.
If an AssertionError is thrown in a try-catch block, the finally block will be bypassed.
D.
It is proper to handle assertion statement failures using a catch (AssertionException ae) block.

Your Answer: Option (Not Answered)

Correct Answer: Option A

Explanation:

Option A is correct because it is sometimes advisable to thrown an assertion error even if assertions have been disabled.

Option B is incorrect because it is considered appropriate to check argument values in private methods using assertions.

Option C is incorrect; finally is never bypassed.

Option D is incorrect because AssertionErrors should never be handled.

Learn more problems on : Assertions

Discuss about this problem : Discuss in Forum

12.

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?

A.
Foo.Bar b = new Foo.Bar();
B.
Foo.Bar b = f.new Bar();
C.
Bar b = new f.Bar();
D.
Bar b = f.new Bar();

Your Answer: Option (Not Answered)

Correct Answer: Option B

Explanation:

Option B is correct because the syntax is correct-using both names (the enclosing class and the inner class) in the reference declaration, then using a reference to the enclosing class to invoke new on the inner class.

Option A, C and D all use incorrect syntax. A is incorrect because it doesn't use a reference to the enclosing class, and also because it includes both names in the new.

C is incorrect because it doesn't use the enclosing class name in the reference variable declaration, and because the new syntax is wrong.

D is incorrect because it doesn't use the enclosing class name in the reference variable declaration.

Learn more problems on : Inner Classes

Discuss about this problem : Discuss in Forum

13.

What is the name of the method used to start a thread execution?

A.
init();
B.
start();
C.
run();
D.
resume();

Your Answer: Option (Not Answered)

Correct Answer: Option B

Explanation:

Option B 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. There is no init() method in the Thread class.

Option C 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 D is wrong. The resume() method is deprecated. It resumes a suspended thread.

Learn more problems on : Threads

Discuss about this problem : Discuss in Forum

14.

Which three are valid method signatures in an interface?

  1. private int getArea();
  2. public float getVol(float x);
  3. public void main(String [] args);
  4. public static void main(String [] args);
  5. boolean setFlag(Boolean [] test);

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

Your Answer: Option (Not Answered)

Correct Answer: Option B

Explanation:

(2), (3), and (5). These are all valid interface method signatures.

(1), is incorrect because an interface method must be public; if it is not explicitly declared public it will be made public implicitly. (4) is incorrect because interface methods cannot be static.

Learn more problems on : Declarations and Access Control

Discuss about this problem : Discuss in Forum

15.

What is the widest valid returnType for methodA in line 3?

public class ReturnIt 
{ 
    returnType methodA(byte x, double y) /* Line 3 */
    { 
        return (long)x / y * 2; 
    } 
}

A.
int
B.
byte
C.
long
D.
double

Your Answer: Option (Not Answered)

Correct Answer: Option D

Explanation:

However A, B and C are all wrong. Each of these would result in a narrowing conversion. Whereas we want a widening conversion, therefore the only correct answer is D. Don't be put off by the long cast, this applies only to the variable x and not the rest of the expression. It is the variable y (of type double) that forces the widening conversion to double.

Java's widening conversions are:

- From a byte to a short, an int, a long, a float, or a double.

- From a short, an int, a long, a float, or a double.

- From a char to an int, a long, a float, or a double.

- From an int to a long, a float, or a double.

- From a long to a float, or a double.

- From a float to a double.

Learn more problems on : Declarations and Access Control

Discuss about this problem : Discuss in Forum

16.

Which of the following class level (nonlocal) variable declarations will not compile?

A.
protected int a;
B.
transient int b = 3;
C.
private synchronized int e;
D.
volatile int d;

Your Answer: Option (Not Answered)

Correct Answer: Option C

Explanation:

Option C will not compile; the synchronized modifier applies only to methods.

Option A and B will compile because protected and transient are legal variable modifiers. Option D will compile because volatile is a proper variable modifier.

Learn more problems on : Declarations and Access Control

Discuss about this problem : Discuss in Forum

17.

interface Base 
{
    boolean m1 ();
    byte m2(short s);
}
which two code fragments will compile?
  1. interface Base2 implements Base {}
  2. abstract class Class2 extends Base
    { public boolean m1(){ return true; }}
  3. abstract class Class2 implements Base {}
  4. abstract class Class2 implements Base
    { public boolean m1(){ return (7 > 4); }}
  5. abstract class Class2 implements Base
    { protected boolean m1(){ return (5 > 7) }}

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

Your Answer: Option (Not Answered)

Correct Answer: Option C

Explanation:

(3) is correct because an abstract class doesn't have to implement any or all of its interface's methods. (4) is correct because the method is correctly implemented ((7 > 4) is a boolean).

(1) is incorrect because interfaces don't implement anything. (2) is incorrect because classes don't extend interfaces. (5) is incorrect because interface methods are implicitly public, so the methods being implemented must be public.

Learn more problems on : Declarations and Access Control

Discuss about this problem : Discuss in Forum

18.

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

A.
I is 1
B.
I is 1 I is 1
C.
No output is produced.
D.
Compilation error

Your Answer: Option (Not Answered)

Correct Answer: Option C

Explanation:

There are two different looping constructs in this problem. The first is a do-while loop and the second is a while loop, nested inside the do-while. The body of the do-while is only a single statement-brackets are not needed. You are assured that the while expression will be evaluated at least once, followed by an evaluation of the do-while expression. Both expressions are false and no output is produced.

Learn more problems on : Flow Control

Discuss about this problem : Discuss in Forum

19.

What will be the output of the program?

class s1 implements Runnable 
{ 
    int x = 0, y = 0; 
    int addX() {x++; return x;} 
    int addY() {y++; return y;} 
    public void run() { 
    for(int i = 0; i < 10; i++) 
        System.out.println(addX() + " " + addY()); 
} 
    public static void main(String args[]) 
    { 
        s1 run1 = new s1(); 
        s1 run2 = new s1(); 
        Thread t1 = new Thread(run1); 
        Thread t2 = new Thread(run2); 
        t1.start(); 
        t2.start(); 
    } 
}

A.
Compile time Error: There is no start() method
B.
Will print in this order: 1 1 2 2 3 3 4 4 5 5...
C.
Will print but not exactly in an order (e.g: 1 1 2 2 1 1 3 3...)
D.
Will print in this order: 1 2 3 4 5 6... 1 2 3 4 5 6...

Your Answer: Option (Not Answered)

Correct Answer: Option C

Explanation:

Both threads are operating on different sets of instance variables. If you modify the code of the run() method to print the thread name it will help to clarify the output:

public void run()
{
for(int i = 0; i < 10; i++)

System.out.println(
Thread.currentThread().getName() + ": " + addX() + " " + addY()
);

}

Learn more problems on : Threads

Discuss about this problem : Discuss in Forum

20.

class Test 
{
    public static void main(String [] args) 
    {
        printAll(args);
    }

    public static void printAll(String[] lines) 
    {
        for(int i = 0; i < lines.length; i++)
        {
            System.out.println(lines[i]);
            Thread.currentThread().sleep(1000);
        }
    }
}
the static method Thread.currentThread() returns a reference to the currently executing Thread object. What is the result of this code?

A.
Each String in the array lines will output, with a 1-second pause.
B.
Each String in the array lines will output, with no pause in between because this method is not executed in a Thread.
C.
Each String in the array lines will output, and there is no guarantee there will be a pause because currentThread() may not retrieve this thread.
D.
This code will not compile.

Your Answer: Option (Not Answered)

Correct Answer: Option D

Explanation:

D. The sleep() method must be enclosed in a try/catch block, or the method printAll() must declare it throws the InterruptedException.

A is incorrect, but it would be correct if the InterruptedException was dealt with.

B is incorrect, but it would still be incorrect if the InterruptedException was dealt with because all Java code, including the main() method, runs in threads.

C is incorrect. The sleep() method is static, so even if it is called on an instance, it still always affects the currently executing thread.

Learn more problems on : Threads

Discuss about this problem : Discuss in Forum

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