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?

int i = 0, j = 5; 
tp: for (;;) 
    {
        i++;  
        for (;;) 
        {
            if(i > --j) 
            {
                break tp; 
            } 
        } 
        System.out.println("i =" + i + ", j = " + j);

A.
i = 1, j = 0
B.
i = 1, j = 4
C.
i = 3, j = 4
D.
Compilation fails.

Your Answer: Option (Not Answered)

Correct Answer: Option D

Explanation:

If you examine the code carefully you will notice a missing curly bracket at the end of the code, this would cause the code to fail.

Learn more problems on : Flow Control

Discuss about this problem : Discuss in Forum

2.

Which is the valid declarations within an interface definition?

A.
public double methoda();
B.
public final double methoda();
C.
static void methoda(double d1);
D.
protected void methoda(double d1);

Your Answer: Option (Not Answered)

Correct Answer: Option A

Explanation:

Option A is correct. A public access modifier is acceptable. The method prototypes in an interface are all abstract by virtue of their declaration, and should not be declared abstract.

Option B is wrong. The final modifier means that this method cannot be constructed in a subclass. A final method cannot be abstract.

Option C is wrong. static is concerned with the class and not an instance.

Option D is wrong. protected is not permitted when declaring a method of an interface. See information below.

Member declarations in an interface disallow the use of some declaration modifiers; you cannot use transient, volatile, or synchronized in a member declaration in an interface. Also, you may not use the private and protected specifiers when declaring members of an interface.

Learn more problems on : Language Fundamentals

Discuss about this problem : Discuss in Forum

3.

Which of the following will produce an answer that is closest in value to a double, d, while not being greater than d?

A.
(int)Math.min(d);
B.
(int)Math.max(d);
C.
(int)Math.abs(d);
D.
(int)Math.floor(d);

Your Answer: Option (Not Answered)

Correct Answer: Option D

Explanation:

The casting to an int is a smokescreen. Use a process of elimination to answer this question:

Option D is the correct answer, it is syntathecially correct and will consistently return a value less than d.

Option A and B are wrong because both the min() and max() methods require 2 arguments whereas here they are passed only one parameter.

Option C is wrong because it could return a value greater than d (if d was negative).

Learn more problems on : Java.lang Class

Discuss about this problem : Discuss in Forum

4.

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

5.

What will be the output of the program?

public static void main(String[] args) 
{
    Object obj = new Object() 
    {
        public int hashCode() 
        {
            return 42;
        }
    }; 
    System.out.println(obj.hashCode()); 
}

A.
42
B.
Runtime Exception
C.
Compile Error at line 2
D.
Compile Error at line 5

Your Answer: Option (Not Answered)

Correct Answer: Option A

Explanation:

This code is an example of an anonymous inner class. They can be declared to extend another class or implement a single interface. Since they have no name you can not use the "new" keyword on them.

In this case the annoynous class is extending the Object class. Within the {} you place the methods you want for that class. After this class has been declared its methods can be used by that object in the usual way e.g. objectname.annoymousClassMethod()

Learn more problems on : Objects and Collections

Discuss about this problem : Discuss in Forum

6.

What will be the output of the program?

import java.util.*; 
class H 
{
    public static void main (String[] args) 
    { 
        Object x = new Vector().elements(); 
        System.out.print((x instanceof Enumeration)+","); 
        System.out.print((x instanceof Iterator)+","); 
        System.out.print(x instanceof ListIterator); 
    } 
}

A.
Prints: false,false,false
B.
Prints: false,false,true
C.
Prints: false,true,false
D.
Prints: true,false,false

Your Answer: Option (Not Answered)

Correct Answer: Option D

Explanation:

The Vector.elements method returns an Enumeration over the elements of the vector. Vector implements the List interface and extends AbstractList so it is also possible to get an Iterator over a Vector by invoking the iterator or listIterator method.

Learn more problems on : Objects and Collections

Discuss about this problem : Discuss in Forum

7.

What will be the output of the program?

import java.util.*; 
class I 
{
    public static void main (String[] args) 
    {
        Object i = new ArrayList().iterator(); 
        System.out.print((i instanceof List)+","); 
        System.out.print((i instanceof Iterator)+","); 
        System.out.print(i instanceof ListIterator); 
    } 
}

A.
Prints: false, false, false
B.
Prints: false, false, true
C.
Prints: false, true, false
D.
Prints: false, true, true

Your Answer: Option (Not Answered)

Correct Answer: Option C

Explanation:

The iterator() method returns an iterator over the elements in the list in proper sequence, it doesn't return a List or a ListIterator object.

A ListIterator can be obtained by invoking the listIterator method.

Learn more problems on : Objects and Collections

Discuss about this problem : Discuss in Forum

8.

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

9.

What will be the output of the program?

public class CommandArgs 
{
    public static void main(String [] args) 
    {
        String s1 = args[1];
        String s2 = args[2];
        String s3 = args[3];
        String s4 = args[4];
        System.out.print(" args[2] = " + s2);
    }
}

and the command-line invocation is

> java CommandArgs 1 2 3 4

A.
args[2] = 2
B.
args[2] = 3
C.
args[2] = null
D.
An exception is thrown at runtime.

Your Answer: Option (Not Answered)

Correct Answer: Option D

Explanation:

An exception is thrown because in the code String s4 = args[4];, the array index (the fifth element) is out of bounds. The exception thrown is the cleverly named ArrayIndexOutOfBoundsException.

Learn more problems on : Language Fundamentals

Discuss about this problem : Discuss in Forum

10.

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

11.

What will be the output of the program?

public class SqrtExample 
{
    public static void main(String [] args) 
    {
        double value = -9.0;
        System.out.println( Math.sqrt(value));
    }
}

A.
3.0
B.
-3.0
C.
NaN
D.
Compilation fails.

Your Answer: Option (Not Answered)

Correct Answer: Option C

Explanation:

The sqrt() method returns NaN (not a number) when it's argument is less than zero.

Learn more problems on : Java.lang Class

Discuss about this problem : Discuss in Forum

12.

What will be the output of the program?

int i = (int) Math.random();

A.
i = 0
B.
i = 1
C.
value of i is undetermined
D.
Statement causes a compile error

Your Answer: Option (Not Answered)

Correct Answer: Option A

Explanation:

Math.random() returns a double value greater than or equal to 0 and less than 1. Its value is stored to an int but as this is a narrowing conversion, a cast is needed to tell the compiler that you are aware that there may be a loss of precision.

The value after the decimal point is lost when you cast a double to int and you are left with 0.

Learn more problems on : Java.lang Class

Discuss about this problem : Discuss in Forum

13.

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

14.

What will be the output of the program?

class A 
{ 
    public A(int x){} 
} 
class B extends A { } 
public class test 
{ 
    public static void main (String args []) 
    {
        A a = new B(); 
        System.out.println("complete"); 
    } 
}

A.
It compiles and runs printing nothing
B.
Compiles but fails at runtime
C.
Compile Error
D.
Prints "complete"

Your Answer: Option (Not Answered)

Correct Answer: Option C

Explanation:

No constructor has been defined for class B therefore it will make a call to the default constructor but since class B extends class A it will also call the Super() default constructor.

Since a constructor has been defined in class A java will no longer supply a default constructor for class A therefore when class B calls class A's default constructor it will result in a compile error.

Learn more problems on : Java.lang Class

Discuss about this problem : Discuss in Forum

15.

/* Missing Statement ? */
public class foo 
{
    public static void main(String[]args)throws Exception 
    {
        java.io.PrintWriter out = new java.io.PrintWriter(); 
        new java.io.OutputStreamWriter(System.out,true); 
        out.println("Hello"); 
    } 
}
What line of code should replace the missing statement to make this program compile?

A.
No statement required.
B.
import java.io.*;
C.
include java.io.*;
D.
import java.io.PrintWriter;

Your Answer: Option (Not Answered)

Correct Answer: Option A

Explanation:

The usual method for using/importing the java packages/classes is by using an import statement at the top of your code. However it is possible to explicitly import the specific class that you want to use as you use it which is shown in the code above. The disadvantage of this however is that every time you create a new object you will have to use the class path in the case "java.io" then the class name in the long run leading to a lot more typing.

Learn more problems on : Objects and Collections

Discuss about this problem : Discuss in Forum

16.

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

17.

Which interface does java.util.Hashtable implement?

A.
Java.util.Map
B.
Java.util.List
C.
Java.util.HashTable
D.
Java.util.Collection

Your Answer: Option (Not Answered)

Correct Answer: Option A

Explanation:

Hash table based implementation of the Map interface.

Learn more problems on : Objects and Collections

Discuss about this problem : Discuss in Forum

18.

What will be the output of the program?

class Test 
{
    public static void main(String [] args) 
    {
        int x=20;
        String sup = (x < 15) ? "small" : (x < 22)? "tiny" : "huge";
        System.out.println(sup);
    }
}

A.
small
B.
tiny
C.
huge
D.
Compilation fails

Your Answer: Option (Not Answered)

Correct Answer: Option B

Explanation:

This is an example of a nested ternary operator. The second evaluation (x < 22) is true, so the "tiny" value is assigned to sup.

Learn more problems on : Operators and Assignments

Discuss about this problem : Discuss in Forum

19.

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

20.

Which two statements are equivalent?

  1. 16*4
  2. 16>>2
  3. 16/2^2
  4. 16>>>2

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

Your Answer: Option (Not Answered)

Correct Answer: Option B

Explanation:

(2) is correct. 16 >> 2 = 4

(4) is correct. 16 >>> 2 = 4

(1) is wrong. 16 * 4 = 64

(3) is wrong. 16/2 ^ 2 = 10

Learn more problems on : Operators and Assignments

Discuss about this problem : Discuss in Forum

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