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.

Given a method in a protected class, what access modifier do you use to restrict access to that method to only the other members of the same class?

A.
final
B.
static
C.
private
D.
protected
E.
volatile

Your Answer: Option (Not Answered)

Correct Answer: Option C

Explanation:

The private access modifier limits access to members of the same class.

Option A, B, D, and E are wrong because protected are the wrong access modifiers, and final, static, and volatile are modifiers but not access modifiers.

Learn more problems on : Declarations and Access Control

Discuss about this problem : Discuss in Forum

2.

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

3.

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

4.

    
import java.awt.*;
class Ticker extends Component 
{
    public static void main (String [] args) 
    {
        Ticker t = new Ticker();
        /* Missing Statements ? */
    }
}
which two of the following statements, inserted independently, could legally be inserted into missing section of this code?
  1. boolean test = (Component instanceof t);
  2. boolean test = (t instanceof Ticker);
  3. boolean test = t.instanceof(Ticker);
  4. boolean test = (t instanceof Component);

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

Your Answer: Option (Not Answered)

Correct Answer: Option D

Explanation:

(2) is correct because class type Ticker is part of the class hierarchy of t; therefore it is a legal use of the instanceof operator. (4) is also correct because Component is part of the hierarchy of t, because Ticker extends Component.

(1) is incorrect because the syntax is wrong. A variable (or null) always appears before the instanceof operator, and a type appears after it. (3) is incorrect because the statement is used as a method (t.instanceof(Ticker);), which is illegal.

Learn more problems on : Operators and Assignments

Discuss about this problem : Discuss in Forum

5.

import java.awt.Button;
class CompareReference 
{
    public static void main(String [] args) 
    {
        float f = 42.0f;
        float [] f1 = new float[2];
        float [] f2 = new float[2];
        float [] f3 = f1;
        long x = 42;
        f1[0] = 42.0f;
    }
}
which three statements are true?
  1. f1 == f2
  2. f1 == f3
  3. f2 == f1[1]
  4. x == f1[0]
  5. f == f1[0]

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

Your Answer: Option (Not Answered)

Correct Answer: Option B

Explanation:

(2) is correct because the reference variables f1 and f3 refer to the same array object.

(4) is correct because it is legal to compare integer and floating-point types.

(5) is correct because it is legal to compare a variable with an array element.

(3) is incorrect because f2 is an array object and f1[1] is an array element.

Learn more problems on : Operators and Assignments

Discuss about this problem : Discuss in Forum

6.

What will be the output of the program?

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

A.
finished
B.
Compiliation fails.
C.
An AssertionError is thrown and finished is output.
D.
An AssertionError is thrown with the message "assertion failed."

Your Answer: Option (Not Answered)

Correct Answer: Option B

Explanation:

Compilation Fails. You can't use the Assert statement in a similar way to the ternary operator. Don't confuse.

Learn more problems on : Assertions

Discuss about this problem : Discuss in Forum

7.

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

8.

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

A.
The code fails to compile in the main() method
B.
The code fails to compile in the run() method
C.
Only the text "In run" will be displayed
D.
The text "In run" followed by "Leaving run" will be displayed

Your Answer: Option (Not Answered)

Correct Answer: Option D

Learn more problems on : Threads

Discuss about this problem : Discuss in Forum

9.

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

A.
Compilation error.
B.
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.
C.
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.
D.
Will print in this order x = 1 y = 2 x = 3 y = 4 x = 5 y = 6 x = 7 y = 8...

Your Answer: Option (Not Answered)

Correct Answer: Option C

Explanation:

Both threads are operating on the same instance variables. Because the code is synchronized the first thread will complete before the second thread begins. Modify line 17 to print the thread names:

System.out.println(Thread.currentThread().getName() + " x = " + x + ", y = " + y);

Learn more problems on : Threads

Discuss about this problem : Discuss in Forum

10.

What will be the output of the program?

public class X 
{
    public static void main(String [] args) 
    {
        String names [] = new String[5];
        for (int x=0; x < args.length; x++)
            names[x] = args[x];
        System.out.println(names[2]);
    }
}

and the command line invocation is

> java X a b

A.
names
B.
null
C.
Compilation fails
D.
An exception is thrown at runtime

Your Answer: Option (Not Answered)

Correct Answer: Option B

Explanation:

The names array is initialized with five null elements. Then elements 0 and 1 are assigned the String values "a" and "b" respectively (the command-line arguments passed to main). Elements of names array 2, 3, and 4 remain unassigned, so they have a value of null.

Learn more problems on : Language Fundamentals

Discuss about this problem : Discuss in Forum

11.

What will be the output of the program?


public class TestDogs 
{
    public static void main(String [] args) 
    {
        Dog [][] theDogs = new Dog[3][];
        System.out.println(theDogs[2][0].toString());
    }
}
class Dog { }

A.
null
B.
theDogs
C.
Compilation fails
D.
An exception is thrown at runtime

Your Answer: Option (Not Answered)

Correct Answer: Option D

Explanation:

The second dimension of the array referenced by theDogs has not been initialized. Attempting to access an uninitialized object element (System.out.println(theDogs[2][0].toString());) raises a NullPointerException.

Learn more problems on : Language Fundamentals

Discuss about this problem : Discuss in Forum

12.

In the given program, how many lines of output will be produced?

public class Test 
{
    public static void main(String [] args) 
    {
    int [] [] [] x = new int [3] [] [];
    int i, j;
    x[0] = new int[4][];
    x[1] = new int[2][];
    x[2] = new int[5][];
    for (i = 0; i < x.length; i++)
    {
        for (j = 0; j < x[i].length; j++) 
        {
            x[i][j] = new int [i + j + 1];
            System.out.println("size = " + x[i][j].length);
        }
    }
    }
}

A.
7
B.
9
C.
11
D.
13
E.
Compilation fails

Your Answer: Option (Not Answered)

Correct Answer: Option C

Explanation:

The loops use the array sizes (length).

It produces 11 lines of output as given below.

D:\Java>javac Test.java

D:\Java>java Test
size = 1
size = 2
size = 3
size = 4
size = 2
size = 3
size = 3
size = 4
size = 5
size = 6
size = 7

Therefore, 11 is the answer.

Learn more problems on : Language Fundamentals

Discuss about this problem : Discuss in Forum

13.

What will be the output of the program ?

public class Test 
{
    public static void main(String [] args) 
    {
        signed int x = 10;
        for (int y=0; y<5; y++, x--)
            System.out.print(x + ", ");
    }
}

A.
10, 9, 8, 7, 6,
B.
9, 8, 7, 6, 5,
C.
Compilation fails.
D.
An exception is thrown at runtime.

Your Answer: Option (Not Answered)

Correct Answer: Option C

Explanation:

The word "signed" is not a valid modifier keyword in the Java language. All number primitives in Java are signed. Hence the Compilation will fails.

Learn more problems on : Language Fundamentals

Discuss about this problem : Discuss in Forum

14.

public class MyProgram 
{
    public static void throwit() 
    {
        throw new RuntimeException();
    }
    public static void main(String args[])
    {
        try 
        {
            System.out.println("Hello world ");
            throwit();
            System.out.println("Done with try block ");
        }
        finally 
        {
            System.out.println("Finally executing ");
        }
    }
}
which answer most closely indicates the behavior of the program?

A.
The program will not compile.
B.
The program will print Hello world, then will print that a RuntimeException has occurred, then will print Done with try block, and then will print Finally executing.
C.
The program will print Hello world, then will print that a RuntimeException has occurred, and then will print Finally executing.
D.
The program will print Hello world, then will print Finally executing, then will print that a RuntimeException has occurred.

Your Answer: Option (Not Answered)

Correct Answer: Option D

Explanation:

Once the program throws a RuntimeException (in the throwit() method) that is not caught, the finally block will be executed and the program will be terminated. If a method does not handle an exception, the finally block is executed before the exception is propagated.

Learn more problems on : Exceptions

Discuss about this problem : Discuss in Forum

15.

What will be the output of the program?

public class NFE 
{
    public static void main(String [] args) 
    {
    String s = "42";
        try 
        {
            s = s.concat(".5");  /* Line 8 */
            double d = Double.parseDouble(s);
            s = Double.toString(d);
            int x = (int) Math.ceil(Double.valueOf(s).doubleValue());
            System.out.println(x);
        }
        catch (NumberFormatException e) 
        {
            System.out.println("bad number");
        }
    }
}

A.
42
B.
42.5
C.
43
D.
bad number

Your Answer: Option (Not Answered)

Correct Answer: Option C

Explanation:

All of this code is legal, and line 8 creates a new String with a value of "42.5". Lines 9 and 10 convert the String to a double and then back again. Line 11 is fun— Math.ceil()'s argument expression is evaluated first. We invoke the valueOf() method that returns an anonymous Double object (with a value of 42.5). Then the doubleValue() method is called (invoked on the newly created Double object), and returns a double primitive (there and back again), with a value of (you guessed it) 42.5. The ceil() method converts this to 43.0, which is cast to an int and assigned to x.

Learn more problems on : Java.lang Class

Discuss about this problem : Discuss in Forum

16.

Which two statements are true about comparing two instances of the same class, given that the equals() and hashCode() methods have been properly overridden?

  1. If the equals() method returns true, the hashCode() comparison == must return true.
  2. If the equals() method returns false, the hashCode() comparison != must return true.
  3. If the hashCode() comparison == returns true, the equals() method must return true.
  4. If the hashCode() comparison == returns true, the equals() method might return true.

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

Your Answer: Option (Not Answered)

Correct Answer: Option A

Explanation:

(1) is a restatement of the equals() and hashCode() contract. (4) is true because if the hashCode() comparison returns ==, the two objects might or might not be equal.

(2) and (3) are incorrect because the hashCode() method is very flexible in its return values, and often two dissimilar objects can return the same hash code value.

Learn more problems on : Objects and Collections

Discuss about this problem : Discuss in Forum

17.

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

18.

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

19.

What will be the output of the program?

class SSBool 
{
    public static void main(String [] args) 
    {
        boolean b1 = true;
        boolean b2 = false;
        boolean b3 = true;
        if ( b1 & b2 | b2 & b3 | b2 ) /* Line 8 */
            System.out.print("ok ");
        if ( b1 & b2 | b2 & b3 | b2 | b1 ) /*Line 10*/
            System.out.println("dokey");
    }
}

A.
ok
B.
dokey
C.
ok dokey
D.
No output is produced
E.
Compilation error

Your Answer: Option (Not Answered)

Correct Answer: Option B

Explanation:

The & operator has a higher precedence than the | operator so that on line 8 b1 and b2 are evaluated together as are b2 & b3. The final b1 in line 10 is what causes that if test to be true. Hence it prints "dokey".

Learn more problems on : Operators and Assignments

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.