Online Java Programming Test - Java Programming Test 8



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.

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

2.

public class While 
{
    public void loop() 
    {
        int x= 0;
        while ( 1 ) /* Line 6 */
        {
            System.out.print("x plus one is " + (x + 1)); /* Line 8 */
        }
    }
}
Which statement is true?

A.
There is a syntax error on line 1.
B.
There are syntax errors on lines 1 and 6.
C.
There are syntax errors on lines 1, 6, and 8.
D.
There is a syntax error on line 6.

Your Answer: Option (Not Answered)

Correct Answer: Option D

Explanation:

Using the integer 1 in the while statement, or any other looping or conditional construct for that matter, will result in a compiler error. This is old C Program syntax, not valid Java.

A, B and C are incorrect because line 1 is valid (Java is case sensitive so While is a valid class name). Line 8 is also valid because an equation may be placed in a String operation as shown.

Learn more problems on : Flow Control

Discuss about this problem : Discuss in Forum

3.

What will be the output of the program?

int I = 0;
    outer:
    while (true) 
    {
        I++;
        inner:
        for (int j = 0; j < 10; j++) 
        {
            I += j;
            if (j == 3)
                continue inner;
            break outer;
        }
        continue outer;
    }
System.out.println(I);

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

Your Answer: Option (Not Answered)

Correct Answer: Option A

Explanation:

The program flows as follows: I will be incremented after the while loop is entered, then I will be incremented (by zero) when the for loop is entered. The if statement evaluates to false, and the continue statement is never reached. The break statement tells the JVM to break out of the outer loop, at which point I is printed and the fragment is done.

Learn more problems on : Flow Control

Discuss about this problem : Discuss in Forum

4.

What will be the output of the program?

public class Test 
{ 
    private static float[] f = new float[2]; 
    public static void main (String[] args) 
    {
        System.out.println("f[0] = " + f[0]); 
    } 
}

A.
f[0] = 0
B.
f[0] = 0.0
C.
Compile Error
D.
Runtime Exception

Your Answer: Option (Not Answered)

Correct Answer: Option B

Explanation:

The choices are between Option A and B, what this question is really testing is your knowledge of default values of an initialized array. This is an array type float i.e. it is a type that uses decimal point numbers therefore its initial value will be 0.0 and not 0

Learn more problems on : Objects and Collections

Discuss about this problem : Discuss in Forum

5.

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

6.

x = 0;
if (x1.hashCode() != x2.hashCode() )  x = x + 1;
if (x3.equals(x4) )  x = x + 10;
if (!x5.equals(x6) ) x = x + 100;
if (x7.hashCode() == x8.hashCode() )  x = x + 1000;
System.out.println("x = " + x);
and assuming that the equals() and hashCode() methods are properly implemented, if the output is "x = 1111", which of the following statements will always be true?

A.
x2.equals(x1)
B.
x3.hashCode() == x4.hashCode()
C.
x5.hashCode() != x6.hashCode()
D.
x8.equals(x7)

Your Answer: Option (Not Answered)

Correct Answer: Option B

Explanation:

By contract, if two objects are equivalent according to the equals() method, then the hashCode() method must evaluate them to be ==.

Option A is incorrect because if the hashCode() values are not equal, the two objects must not be equal.

Option C is incorrect because if equals() is not true there is no guarantee of any result from hashCode().

Option D is incorrect because hashCode() will often return == even if the two objects do not evaluate to equals() being true.

Learn more problems on : Objects and Collections

Discuss about this problem : Discuss in Forum

7.

class Boo 
{
    Boo(String s) { }
    Boo() { }
}
class Bar extends Boo 
{
    Bar() { }
    Bar(String s) {super(s);}
    void zoo() 
    {
    // insert code here
    }
}
which one create an anonymous inner class from within class Bar?

A.
Boo f = new Boo(24) { };
B.
Boo f = new Bar() { };
C.
Bar f = new Boo(String s) { };
D.
Boo f = new Boo.Bar(String s) { };

Your Answer: Option (Not Answered)

Correct Answer: Option B

Explanation:

Option B is correct because anonymous inner classes are no different from any other class when it comes to polymorphism. That means you are always allowed to declare a reference variable of the superclass type and have that reference variable refer to an instance of a subclass type, which in this case is an anonymous subclass of Bar. Since Bar is a subclass of Boo, it all works.

Option A is incorrect because it passes an int to the Boo constructor, and there is no matching constructor in the Boo class.

Option C is incorrect because it violates the rules of polymorphism—you cannot refer to a superclass type using a reference variable declared as the subclass type. The superclass is not guaranteed to have everything the subclass has.

Option D uses incorrect syntax.

Learn more problems on : Inner Classes

Discuss about this problem : Discuss in Forum

8.

Which statement is true about a static nested class?

A.
You must have a reference to an instance of the enclosing class in order to instantiate it.
B.
It does not have access to nonstatic members of the enclosing class.
C.
It's variables and methods must be static.
D.
It must extend the enclosing class.

Your Answer: Option (Not Answered)

Correct Answer: Option B

Explanation:

Option B is correct because a static nested class is not tied to an instance of the enclosing class, and thus can't access the nonstatic members of the class (just as a static method can't access nonstatic members of a class).

Option A is incorrect because static nested classes do not need (and can't use) a reference to an instance of the enclosing class.

Option C is incorrect because static nested classes can declare and define nonstatic members.

Option D is wrong because it just is. There's no rule that says an inner or nested class has to extend anything.

Learn more problems on : Inner Classes

Discuss about this problem : Discuss in Forum

9.

Which constructs an anonymous inner class instance?

A.
Runnable r = new Runnable() { };
B.
Runnable r = new Runnable(public void run() { });
C.
Runnable r = new Runnable { public void run(){}};
D.
System.out.println(new Runnable() {public void run() { }});

Your Answer: Option (Not Answered)

Correct Answer: Option D

Explanation:

D is correct. It defines an anonymous inner class instance, which also means it creates an instance of that new anonymous class at the same time. The anonymous class is an implementer of the Runnable interface, so it must override the run() method of Runnable.

A is incorrect because it doesn't override the run() method, so it violates the rules of interface implementation.

B and C use incorrect syntax.

Learn more problems on : Inner Classes

Discuss about this problem : Discuss in Forum

10.

What will be the output of the program?

public class HorseTest 
{
    public static void main (String [] args) 
    {
        class Horse 
        {
            public String name; /* Line 7 */
            public Horse(String s) 
            {
                name = s;
            }
        } /* class Horse ends */
        
        Object obj = new Horse("Zippo"); /* Line 13 */
        Horse h = (Horse) obj; /* Line 14 */
        System.out.println(h.name);
    }
} /* class HorseTest ends */

A.
An exception occurs at runtime at line 10.
B.
It prints "Zippo".
C.
Compilation fails because of an error on line 7.
D.
Compilation fails because of an error on line 13.

Your Answer: Option (Not Answered)

Correct Answer: Option B

Explanation:

The code in the HorseTest class is perfectly legal. Line 13 creates an instance of the method-local inner class Horse, using a reference variable declared as type Object. Line 14 casts the Horse object to a Horse reference variable, which allows line 15 to compile. If line 14 were removed, the HorseTest code would not compile, because class Object does not have a name variable.

Learn more problems on : Inner Classes

Discuss about this problem : Discuss in Forum

11.

What will be the output of the program?

public abstract class AbstractTest 
{
    public int getNum() 
    {
        return 45;
    }
    public abstract class Bar 
    {
        public int getNum() 
        {
            return 38;
        }
    }
    public static void main (String [] args) 
    {
        AbstractTest t = new AbstractTest() 
        {
            public int getNum() 
            {
                return 22;
            }
        };
        AbstractTest.Bar f = t.new Bar() 
        {
            public int getNum() 
            {
                return 57;
            }
        };
        
        System.out.println(f.getNum() + " " + t.getNum());
    }
}

A.
57 22
B.
45 38
C.
45 57
D.
An exception occurs at runtime.

Your Answer: Option (Not Answered)

Correct Answer: Option A

Explanation:

You can define an inner class as abstract, which means you can instantiate only concrete subclasses of the abstract inner class. The object referenced by the variable t is an instance of an anonymous subclass of AbstractTest, and the anonymous class overrides the getNum() method to return 22. The variable referenced by f is an instance of an anonymous subclass of Bar, and the anonymous Bar subclass also overrides the getNum() method (to return 57). Remember that to instantiate a Bar instance, we need an instance of the enclosing AbstractTest class to tie to the new Bar inner class instance. AbstractTest can't be instantiated because it's abstract, so we created an anonymous subclass (non-abstract) and then used the instance of that anonymous subclass to tie to the new Bar subclass instance.

Learn more problems on : Inner Classes

Discuss about this problem : Discuss in Forum

12.

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

13.

What will be the output of the program?

class MyThread extends Thread 
{
    public static void main(String [] args) 
    {
        MyThread t = new MyThread(); /* Line 5 */
        t.run();  /* Line 6 */
    }

    public void run() 
    {
        for(int i=1; i < 3; ++i) 
        {
            System.out.print(i + "..");
        }
    }
}

A.
This code will not compile due to line 5.
B.
This code will not compile due to line 6.
C.
1..2..
D.
1..2..3..

Your Answer: Option (Not Answered)

Correct Answer: Option C

Explanation:

Line 6 calls the run() method, so the run() method executes as a normal method should and it prints "1..2.."

A is incorrect because line 5 is the proper way to create an object.

B is incorrect because it is legal to call the run() method, even though this will not start a true thread of execution. The code after line 6 will not execute until the run() method is complete.

D is incorrect because the for loop only does two iterations.

Learn more problems on : Threads

Discuss about this problem : Discuss in Forum

14.

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

15.

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

16.

What allows the programmer to destroy an object x?

A.
x.delete()
B.
x.finalize()
C.
Runtime.getRuntime().gc()
D.
Only the garbage collection system can destroy an object.

Your Answer: Option (Not Answered)

Correct Answer: Option D

Explanation:

Option D is correct. When an object is no longer referenced, it may be reclaimed by the garbage collector. If an object declares a finalizer, the finalizer is executed before the object is reclaimed to give the object a last chance to clean up resources that would not otherwise be released. When a class is no longer needed, it may be unloaded.

Option A is wrong. I found 4 delete() methods in all of the Java class structure. They are:

  1. delete() - Method in class java.io.File : Deletes the file or directory denoted by this abstract pathname.
  2. delete(int, int) - Method in class java.lang.StringBuffer : Removes the characters in a substring of this StringBuffer.
  3. delete(int, int) - Method in interface javax.accessibility.AccessibleEditableText : Deletes the text between two indices
  4. delete(int, int) - Method in class : javax.swing.text.JTextComponent.AccessibleJTextComponent; Deletes the text between two indices

None of these destroy the object to which they belong.

Option B is wrong. I found 19 finalize() methods. The most interesting, from this questions point of view, was the finalize() method in class java.lang.Object which is called by the garbage collector on an object when garbage collection determines that there are no more references to the object. This method does not destroy the object to which it belongs.

Option C is wrong. But it is interesting. The Runtime class has many methods, two of which are:

  1. getRuntime() - Returns the runtime object associated with the current Java application.
  2. gc() - Runs the garbage collector. Calling this method suggests that the Java virtual machine expend effort toward recycling unused objects in order to make the memory they currently occupy available for quick reuse. When control returns from the method call, the virtual machine has made its best effort to recycle all discarded objects. Interesting as this is, it doesn't destroy the object.

Learn more problems on : Garbage Collections

Discuss about this problem : Discuss in Forum

17.

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

18.

What will be the output of the program?

public class WrapTest 
{
    public static void main(String [] args) 
    {
        int result = 0;
        short s = 42;
        Long x = new Long("42");
        Long y = new Long(42);
        Short z = new Short("42");
        Short x2 = new Short(s);
        Integer y2 = new Integer("42");
        Integer z2 = new Integer(42);

        if (x == y) /* Line 13 */
            result = 1;
        if (x.equals(y) ) /* Line 15 */
            result = result + 10;
        if (x.equals(z) ) /* Line 17 */
            result = result + 100;
        if (x.equals(x2) ) /* Line 19 */
            result = result + 1000;
        if (x.equals(z2) ) /* Line 21 */
            result = result + 10000;

        System.out.println("result = " + result);
    }
}

A.
result = 1
B.
result = 10
C.
result = 11
D.
result = 11010

Your Answer: Option (Not Answered)

Correct Answer: Option B

Explanation:

Line 13 fails because == compares reference values, not object values. Line 15 succeeds because both String and primitive wrapper constructors resolve to the same value (except for the Character wrapper). Lines 17, 19, and 21 fail because the equals() method fails if the object classes being compared are different and not in the same tree hierarchy.

Learn more problems on : Java.lang Class

Discuss about this problem : Discuss in Forum

19.

What will be the output of the program?

class Q207 
{ 
    public static void main(String[] args) 
    {
        int i1 = 5; 
        int i2 = 6; 
        String s1 = "7"; 
        System.out.println(i1 + i2 + s1); /* Line 8 */
    } 
}

A.
18
B.
117
C.
567
D.
Compiler error

Your Answer: Option (Not Answered)

Correct Answer: Option B

Explanation:

This question is about the + (plus) operator and the overriden + (string cocatanation) operator. The rules that apply when you have a mixed expression of numbers and strings are:

If either operand is a String, the + operator concatenates the operands.

If both operands are numeric, the + operator adds the operands.

The expression on line 6 above can be read as "Add the values i1 and i2 together, then take the sum and convert it to a string and concatenate it with the String from the variable s1". In code, the compiler probably interprets the expression on line 8 above as:

System.out.println( new StringBuffer() 
    .append(new Integer(i1 + i2).toString()) 
    .append(s1) 
    .toString() ); 

Learn more problems on : Java.lang Class

Discuss about this problem : Discuss in Forum

20.

What will be the output of the program?

public class StringRef 
{
    public static void main(String [] args) 
    {
        String s1 = "abc";
        String s2 = "def";
        String s3 = s2;   /* Line 7 */
        s2 = "ghi";
        System.out.println(s1 + s2 + s3);
    }
}

A.
abcdefghi
B.
abcdefdef
C.
abcghidef
D.
abcghighi

Your Answer: Option (Not Answered)

Correct Answer: Option C

Explanation:

After line 7 executes, both s2 and s3 refer to a String object that contains the value "def". When line 8 executes, a new String object is created with the value "ghi", to which s2 refers. The reference variable s3 still refers to the (immutable) String object with the value "def".

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.