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.

public interface Foo 
{ 
    int k = 4; /* Line 3 */
}
Which three piece of codes are equivalent to line 3?
  1. final int k = 4;
  2. public int k = 4;
  3. static int k = 4;
  4. abstract int k = 4;
  5. volatile int k = 4;
  6. protected int k = 4;

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

Your Answer: Option (Not Answered)

Correct Answer: Option A

Explanation:

(1), (2) and (3) are correct. Interfaces can have constants, which are always implicitly public, static, and final. Interface constant declarations of public, static, and final are optional in any combination.

Learn more problems on : Language Fundamentals

Discuss about this problem : Discuss in Forum


2.

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


3.

Which class or interface defines the wait(), notify(),and notifyAll() methods?

A.
Object
B.
Thread
C.
Runnable
D.
Class

Your Answer: Option (Not Answered)

Correct Answer: Option A

Explanation:

The Object class defines these thread-specific methods.

Option B, C, and D are incorrect because they do not define these methods. And yes, the Java API does define a class called Class, though you do not need to know it for the exam.

Learn more problems on : Threads

Discuss about this problem : Discuss in Forum


4.

public class X 
{
    public static void main(String [] args) 
    {
        X x = new X();
        X x2 = m1(x); /* Line 6 */
        X x4 = new X();
        x2 = x4; /* Line 8 */
        doComplexStuff();
    }
    static X m1(X mx) 
    {
        mx = new X();
        return mx;
    }
}
After line 8 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 B

Explanation:

By the time line 8 has run, the only object without a reference is the one generated as a result of line 6. Remember that "Java is pass by value," so the reference variable x is not affected by the m1() method.

Ref: http://www.javaworld.com/javaworld/javaqa/2000-05/03-qa-0526-pass.html

Learn more problems on : Garbage Collections

Discuss about this problem : Discuss in Forum


5.

What will be the output of the program?

public class Test178 
{ 
    public static void main(String[] args) 
    {
        String s = "foo"; 
        Object o = (Object)s; 
        if (s.equals(o)) 
        { 
            System.out.print("AAA"); 
        } 
        else 
        {
            System.out.print("BBB"); 
        } 
        if (o.equals(s)) 
        {
            System.out.print("CCC"); 
        } 
        else 
        {
            System.out.print("DDD"); 
        } 
    } 
}

A.
AAACCC
B.
AAADDD
C.
BBBCCC
D.
BBBDDD

Your Answer: Option (Not Answered)

Correct Answer: Option A

Learn more problems on : Java.lang Class

Discuss about this problem : Discuss in Forum


6.

What will be the output of the program?

public class Example 
{
    public static void main(String [] args) 
    {
        double values[] = {-2.3, -1.0, 0.25, 4};
        int cnt = 0;
        for (int x=0; x < values.length; x++) 
        {
            if (Math.round(values[x] + .5) == Math.ceil(values[x])) 
            {
                ++cnt;
            }
        }
        System.out.println("same results " + cnt + " time(s)");
    }
}

A.
same results 0 time(s)
B.
same results 2 time(s)
C.
same results 4 time(s)
D.
Compilation fails.

Your Answer: Option (Not Answered)

Correct Answer: Option B

Explanation:

Math.round() adds .5 to the argument then performs a floor(). Since the code adds an additional .5 before round() is called, it's as if we are adding 1 then doing a floor(). The values that start out as integer values will in effect be incremented by 1 on the round() side but not on the ceil() side, and the noninteger values will end up equal.

Learn more problems on : Java.lang Class

Discuss about this problem : Discuss in Forum


7.

What will be the output of the program?

class Tree { } 
class Pine extends Tree { } 
class Oak extends Tree { } 
public class Forest1 
{ 
    public static void main (String [] args)
    { 
        Tree tree = new Pine(); 
        if( tree instanceof Pine ) 
            System.out.println ("Pine"); 
        else if( tree instanceof Tree ) 
            System.out.println ("Tree"); 
        else if( tree instanceof Oak ) 
            System.out.println ( "Oak" ); 
        else 
            System.out.println ("Oops "); 
    } 
}

A.
Pine
B.
Tree
C.
Forest
D.
Oops

Your Answer: Option (Not Answered)

Correct Answer: Option A

Explanation:

The program prints "Pine".

Learn more problems on : Java.lang Class

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.

What will be the output of the program?

public class Test 
{
    public static int y;
    public static void foo(int x) 
    {
        System.out.print("foo ");
        y = x;
    }
    public static int bar(int z) 
    {
        System.out.print("bar ");
        return y = z;
    }
    public static void main(String [] args ) 
    {
        int t = 0;
        assert t > 0 : bar(7);
        assert t > 1 : foo(8); /* Line 18 */
        System.out.println("done ");
    }
}

A.
bar
B.
bar done
C.
foo done
D.
Compilation fails

Your Answer: Option (Not Answered)

Correct Answer: Option D

Explanation:

The foo() method returns void. It is a perfectly acceptable method, but because it returns void it cannot be used in an assert statement, so line 18 will not compile.

Learn more problems on : Assertions

Discuss about this problem : Discuss in Forum


10.

What will be the output of the program?

class s1 extends Thread
{ 
    public void run() 
    { 
        for(int i = 0; i < 3; i++) 
        { 
            System.out.println("A"); 
            System.out.println("B"); 
        } 
    } 
} 
class Test120 extends Thread 
{ 
    public void run() 
    { 
        for(int i = 0; i < 3; i++) 
        { 
            System.out.println("C"); 
            System.out.println("D"); 
        } 
    } 
    public static void main(String args[]) 
        { 
        s1 t1 = new s1(); 
        Test120 t2 = new Test120(); 
        t1.start(); 
        t2.start(); 
    } 
}

A.
Compile time Error There is no start() method
B.
Will print in this order AB CD AB...
C.
Will print but not be able to predict the Order
D.
Will print in this order ABCD...ABCD...

Your Answer: Option (Not Answered)

Correct Answer: Option C

Explanation:

We cannot predict the order in which threads are going to run.

Learn more problems on : Threads

Discuss about this problem : Discuss in Forum


11.

You want a class to have access to members of another class in the same package. Which is the most restrictive access that accomplishes this objective?

A.
public
B.
private
C.
protected
D.
default access

Your Answer: Option (Not Answered)

Correct Answer: Option D

Explanation:

The only two real contenders are C and D. Protected access Option C makes a member accessible only to classes in the same package or subclass of the class. While default access Option D makes a member accessible only to classes in the same package.

Learn more problems on : Declarations and Access Control

Discuss about this problem : Discuss in Forum


12.

Which two of the following are legal declarations for nonnested classes and interfaces?

  1. final abstract class Test {}
  2. public static interface Test {}
  3. final public class Test {}
  4. protected abstract class Test {}
  5. protected interface Test {}
  6. abstract public class Test {}

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

Your Answer: Option (Not Answered)

Correct Answer: Option C

Explanation:

(3), (6). Both are legal class declarations.

(1) is wrong because a class cannot be abstract and final—there would be no way to use such a class. (2) is wrong because interfaces and classes cannot be marked as static. (4) and (5) are wrong because classes and interfaces cannot be marked as protected.

Learn more problems on : Declarations and Access Control

Discuss about this problem : Discuss in Forum


13.

Which two cause a compiler error?

  1. float[ ] f = new float(3);
  2. float f2[ ] = new float[ ];
  3. float[ ]f1 = new float[3];
  4. float f3[ ] = new float[3];
  5. float f5[ ] = {1.0f, 2.0f, 2.0f};

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

Your Answer: Option (Not Answered)

Correct Answer: Option D

Explanation:

(1) causes two compiler errors ( '[' expected and illegal start of expression) because the wrong type of bracket is used, ( ) instead of [ ]. The following is the correct syntax: float[ ] f = new float[3];

(2) causes a compiler error ( '{' expected ) because the array constructor does not specify the number of elements in the array. The following is the correct syntax: float f2[ ] = new float[3];

(3), (4), and (5) compile without error.

Learn more problems on : Declarations and Access Control

Discuss about this problem : Discuss in Forum


14.

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


15.

Which class does not override the equals() and hashCode() methods, inheriting them directly from class Object?

A.
java.lang.String
B.
java.lang.Double
C.
java.lang.StringBuffer
D.
java.lang.Character

Your Answer: Option (Not Answered)

Correct Answer: Option C

Explanation:

java.lang.StringBuffer is the only class in the list that uses the default methods provided by class Object.

Learn more problems on : Objects and Collections

Discuss about this problem : Discuss in Forum


16.

Which statement is true?

A.
All objects that are eligible for garbage collection will be garbage collected by the garbage collector.
B.
Objects with at least one reference will never be garbage collected.
C.
Objects from a class with the finalize() method overridden will never be garbage collected.
D.
Objects instantiated within anonymous inner classes are placed in the garbage collectible heap.

Your Answer: Option (Not Answered)

Correct Answer: Option D

Explanation:

All objects are placed in the garbage collectible heap.

Option A is incorrect because the garbage collector makes no guarantees.

Option B is incorrect because islands of isolated objects can exist.

Option C is incorrect because finalize() has no such mystical powers.

Learn more problems on : Garbage Collections

Discuss about this problem : Discuss in Forum


17.

Which statement is true?

A.
Calling Runtime.gc() will cause eligible objects to be garbage collected.
B.
The garbage collector uses a mark and sweep algorithm.
C.
If an object can be accessed from a live thread, it can't be garbage collected.
D.
If object 1 refers to object 2, then object 2 can't be garbage collected.

Your Answer: Option (Not Answered)

Correct Answer: Option C

Explanation:

This is a great way to think about when objects can be garbage collected.

Option A and B assume guarantees that the garbage collector never makes.

Option D is wrong because of the now famous islands of isolation scenario.

Learn more problems on : Garbage Collections

Discuss about this problem : Discuss in Forum


18.

What will be the output of the program?

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

A.
BD
B.
BCD
C.
BDE
D.
BCDE

Your Answer: Option (Not Answered)

Correct Answer: Option C

Explanation:

A Run time exception is thrown and caught in the catch statement on line 10. All the code after the finally statement is run because the exception has been caught.

Learn more problems on : Exceptions

Discuss about this problem : Discuss in Forum


19.

Which of the following statements is true?

A.
In an assert statement, the expression after the colon ( : ) can be any Java expression.
B.
If a switch block has no default, adding an assert default is considered appropriate.
C.
In an assert statement, if the expression after the colon ( : ) does not have a value, the assert's error message will be empty.
D.
It is appropriate to handle assertion failures using a catch clause.

Your Answer: Option (Not Answered)

Correct Answer: Option B

Explanation:

Adding an assertion statement to a switch statement that previously had no default case is considered an excellent use of the assert mechanism.

Option A is incorrect because only Java expressions that return a value can be used. For instance, a method that returns void is illegal.

Option C is incorrect because the expression after the colon must have a value.

Option D is incorrect because assertions throw errors and not exceptions, and assertion errors do cause program termination and should not be handled.

Learn more problems on : Assertions

Discuss about this problem : Discuss in Forum


20.

What will be the output of the program?

public class Test 
{
    public int aMethod()
    {
        static int i = 0;
        i++;
        return i;
    }
    public static void main(String args[])
    {
        Test test = new Test();
        test.aMethod();
        int j = test.aMethod();
        System.out.println(j);
    }
}

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

Your Answer: Option (Not Answered)

Correct Answer: Option D

Explanation:

Compilation failed because static was an illegal start of expression - method variables do not have a modifier (they are always considered local).

Learn more problems on : Declarations and Access Control

Discuss about this problem : Discuss in Forum


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