Online Java Programming Test - Java Programming Test 10



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?

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

2.

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

3.

What will be the output of the program?

class Equals 
{
    public static void main(String [] args) 
    {
        int x = 100;
        double y = 100.1;
        boolean b = (x = y); /* Line 7 */
        System.out.println(b);
    }
}

A.
true
B.
false
C.
Compilation fails
D.
An exception is thrown at runtime

Your Answer: Option (Not Answered)

Correct Answer: Option C

Explanation:

The code will not compile because in line 7, the line will work only if we use (x==y) in the line. The == operator compares values to produce a boolean, whereas the = operator assigns a value to variables.

Option A, B, and D are incorrect because the code does not get as far as compiling. If we corrected this code, the output would be false.

Learn more problems on : Operators and Assignments

Discuss about this problem : Discuss in Forum

4.

What will be the output of the program?

class Bitwise 
{
    public static void main(String [] args) 
    {
        int x = 11 & 9;
        int y = x ^ 3;
        System.out.println( y | 12 );
    }
}

A.
0
B.
7
C.
8
D.
14

Your Answer: Option (Not Answered)

Correct Answer: Option D

Explanation:

The & operator produces a 1 bit when both bits are 1. The result of the & operation is 9. The ^ operator produces a 1 bit when exactly one bit is 1; the result of this operation is 10. The | operator produces a 1 bit when at least one bit is 1; the result of this operation is 14.

Learn more problems on : Operators and Assignments

Discuss about this problem : Discuss in Forum

5.

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

6.

What will be the output of the program?

int x = l, y = 6; 
while (y--) 
{
    x++; 
} 
System.out.println("x = " + x +" y = " + y);

A.
x = 6 y = 0
B.
x = 7 y = 0
C.
x = 6 y = -1
D.
Compilation fails.

Your Answer: Option (Not Answered)

Correct Answer: Option D

Explanation:

Compilation fails because the while loop demands a boolean argument for it's looping condition, but in the code, it's given an int argument.

while(true) { //insert code here }

Learn more problems on : Flow Control

Discuss about this problem : Discuss in Forum

7.

What will be the output of the program?

for (int i = 0; i < 4; i += 2) 
{ 
    System.out.print(i + " "); 
} 
System.out.println(i); /* Line 5 */

A.
0 2 4
B.
0 2 4 5
C.
0 1 2 3 4
D.
Compilation fails.

Your Answer: Option (Not Answered)

Correct Answer: Option D

Explanation:

Compilation fails on the line 5 - System.out.println(i); as the variable i has only been declared within the for loop. It is not a recognised variable outside the code block of loop.

Learn more problems on : Flow Control

Discuss about this problem : Discuss in Forum

8.

What will be the output of the program?

int x = 3; 
int y = 1; 
if (x = y) /* Line 3 */
{
    System.out.println("x =" + x); 
}

A.
x = 1
B.
x = 3
C.
Compilation fails.
D.
The code runs with no output.

Your Answer: Option (Not Answered)

Correct Answer: Option C

Explanation:

Line 3 uses an assignment as opposed to comparison. Because of this, the if statement receives an integer value instead of a boolean. And so the compilation fails.

Learn more problems on : Flow Control

Discuss about this problem : Discuss in Forum

9.

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

10.

public class ExceptionTest 
{ 
    class TestException extends Exception {} 
    public void runTest() throws TestException {} 
    public void test() /* Point X */ 
    { 
        runTest(); 
    } 
}
At Point X on line 5, which code is necessary to make the code compile?

A.
No code is necessary.
B.
throws Exception
C.
catch ( Exception e )
D.
throws RuntimeException

Your Answer: Option (Not Answered)

Correct Answer: Option B

Explanation:

Option B is correct. This works because it DOES throw an exception if an error occurs.

Option A is wrong. If you compile the code as given the compiler will complain:

"unreported exception must be caught or declared to be thrown" The class extends Exception so we are forced to test for exceptions.

Option C is wrong. The catch statement belongs in a method body not a method specification.

Option D is wrong. TestException is a subclass of Exception therefore the test method, in this example, must throw TestException or some other class further up the Exception tree. Throwing RuntimeException is just not on as this belongs in the java.lang.RuntimeException branch (it is not a superclass of TestException). The compiler complains with the same error as in A above.

Learn more problems on : Exceptions

Discuss about this problem : Discuss in Forum

11.

You need to store elements in a collection that guarantees that no duplicates are stored. Which one of the following interfaces provide that capability?

A.
Java.util.Map
B.
Java.util.List
C.
Java.util.Collection
D.
None of the above

Your Answer: Option (Not Answered)

Correct Answer: Option A

Explanation:

Option A is correct. A Map cannot contain duplicate keys.

Option B is wrong. Lists typically allow duplicate elements.

Option C is wrong. Collection allows duplicate elements.

Learn more problems on : Objects and Collections

Discuss about this problem : Discuss in Forum

12.

Which collection class allows you to access its elements by associating a key with an element's value, and provides synchronization?

A.
java.util.SortedMap
B.
java.util.TreeMap
C.
java.util.TreeSet
D.
java.util.Hashtable

Your Answer: Option (Not Answered)

Correct Answer: Option D

Explanation:

Hashtable is the only class listed that provides synchronized methods. If you need synchronization great; otherwise, use HashMap, it's faster.

Learn more problems on : Objects and Collections

Discuss about this problem : Discuss in Forum

13.

What is the numerical range of char?

A.
0 to 32767
B.
0 to 65535
C.
-256 to 255
D.
-32768 to 32767

Your Answer: Option (Not Answered)

Correct Answer: Option B

Explanation:

The char type is integral but unsigned. The range of a variable of type char is from 0 to 216-1 or 0 to 65535. Java characters are Unicode, which is a 16-bit encoding capable of representing a wide range of international characters. If the most significant nine bits of a char are 0, then the encoding is the same as seven-bit ASCII.

Learn more problems on : Objects and Collections

Discuss about this problem : Discuss in Forum

14.

Which of the following are true statements?

  1. The Iterator interface declares only three methods: hasNext, next and remove.
  2. The ListIterator interface extends both the List and Iterator interfaces.
  3. The ListIterator interface provides forward and backward iteration capabilities.
  4. The ListIterator interface provides the ability to modify the List during iteration.
  5. The ListIterator interface provides the ability to determine its position in the List.

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

Your Answer: Option (Not Answered)

Correct Answer: Option B

Explanation:

The ListIterator interface extends the Iterator interface and declares additional methods to provide forward and backward iteration capabilities, List modification capabilities, and the ability to determine the position of the iterator in the List.

Learn more problems on : Objects and Collections

Discuss about this problem : Discuss in Forum

15.

Which method must be defined by a class implementing the java.lang.Runnable interface?

A.
void run()
B.
public void run()
C.
public void start()
D.
void run(int priority)

Your Answer: Option (Not Answered)

Correct Answer: Option B

Explanation:

Option B is correct because in an interface all methods are abstract by default therefore they must be overridden by the implementing class. The Runnable interface only contains 1 method, the void run() method therefore it must be implemented.

Option A and D are incorrect because they are narrowing the access privileges i.e. package(default) access is narrower than public access.

Option C is not method in the Runnable interface therefore it is incorrect.

Learn more problems on : Threads

Discuss about this problem : Discuss in Forum

16.

Which two statements are true?

  1. Deadlock will not occur if wait()/notify() is used
  2. A thread will resume execution as soon as its sleep duration expires.
  3. Synchronization can prevent two objects from being accessed by the same thread.
  4. The wait() method is overloaded to accept a duration.
  5. The notify() method is overloaded to accept a duration.
  6. Both wait() and notify() must be called from a synchronized context.

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

Your Answer: Option (Not Answered)

Correct Answer: Option C

Explanation:

Statements (4) and (6) are correct. (4) is correct because the wait() method is overloaded to accept a wait duration in milliseconds. If the thread has not been notified by the time the wait duration has elapsed, then the thread will move back to runnable even without having been notified.

(6) is correct because wait()/notify()/notifyAll() must all be called from within a synchronized, context. A thread must own the lock on the object its invoking wait()/notify()/notifyAll() on.

(1) is incorrect because wait()/notify() will not prevent deadlock.

(2) is incorrect because a sleeping thread will return to runnable when it wakes up, but it might not necessarily resume execution right away. To resume executing, the newly awakened thread must still be moved from runnable to running by the scheduler.

(3) is incorrect because synchronization prevents two or more threads from accessing the same object.

(5) is incorrect because notify() is not overloaded to accept a duration.

Learn more problems on : Threads

Discuss about this problem : Discuss in Forum

17.

Which of the following statements is true?

A.
If assertions are compiled into a source file, and if no flags are included at runtime, assertions will execute by default.
B.
As of Java version 1.4, assertion statements are compiled by default.
C.
With the proper use of runtime arguments, it is possible to instruct the VM to disable assertions for a certain class, and to enable assertions for a certain package, at the same time.
D.
When evaluating command-line arguments, the VM gives -ea flags precedence over -da flags.

Your Answer: Option (Not Answered)

Correct Answer: Option C

Explanation:

Option C is true because multiple VM flags can be used on a single invocation of a Java program.

Option A is incorrect because at runtime assertions are ignored by default.

Option B is incorrect because as of Java 1.4 you must add the argument -source 1.4 to the command line if you want the compiler to compile assertion statements.

Option D is incorrect because the VM evaluates all assertion flags left to right.

Learn more problems on : Assertions

Discuss about this problem : Discuss in Forum

18.

What will be the output of the program?

try 
{
    Float f1 = new Float("3.0");
    int x = f1.intValue();
    byte b = f1.byteValue();
    double d = f1.doubleValue();
    System.out.println(x + b + d);
}
catch (NumberFormatException e) /* Line 9 */
{
    System.out.println("bad number"); /* Line 11 */
}

A.
9.0
B.
bad number
C.
Compilation fails on line 9.
D.
Compilation fails on line 11.

Your Answer: Option (Not Answered)

Correct Answer: Option A

Explanation:

The xxxValue() methods convert any numeric wrapper object's value to any primitive type. When narrowing is necessary, significant bits are dropped and the results are difficult to calculate.

Learn more problems on : Java.lang Class

Discuss about this problem : Discuss in Forum

19.

What will be the output of the program?

public class ExamQuestion6 
{
    static int x; 
    boolean catch()
    {
        x++; 
        return true; 
    } 
    public static void main(String[] args)
    {
        x=0; 
        if ((catch() | catch()) || catch()) 
            x++; 
        System.out.println(x); 
    } 
}

A.
1
B.
2
C.
3
D.
Compilation Fails

Your Answer: Option (Not Answered)

Correct Answer: Option D

Explanation:

Initially this looks like a question about the logical and logical shortcut operators "|" and "||" but on closer inspection it should be noticed that the name of the boolean method in this code is "catch". "catch" is a reserved keyword in the Java language and cannot be used as a method name. Hence Compilation will fail.

Learn more problems on : Java.lang Class

Discuss about this problem : Discuss in Forum

20.

What will be the output of the program?

String s = "hello"; 
Object o = s; 
if( o.equals(s) )
{
    System.out.println("A"); 
} 
else
{
    System.out.println("B"); 
} 
if( s.equals(o) )
{
    System.out.println("C"); 
} 
else
{ 
    System.out.println("D"); 
}
  1. A
  2. B
  3. C
  4. D

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

Your Answer: Option (Not Answered)

Correct Answer: Option A

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.