Java Practice Test - 3

Java Practice Test - 3

25 Questions Published

Questions

Question 1 Multiple Choice (Single Answer)

After the following code fragment, what is the value in fname?
String str;
int fname;
str = "Foolish boy.";
fname = str.indexOf("fool");

  1. 0
  2. 2
  3. -1
  4. 4
Question 2 Multiple Choice (Single Answer)

Which of the following is not a return type?

  1. boolean
  2. void
  3. public
  4. Button
Question 3 Multiple Choice (Single Answer)

What is the value of 'number' after the following code fragment execution?
int number = 0;
int number2 = 12
while (number < number2)
{
number = number + 1;
}

  1. 5
  2. 12
  3. 21
  4. 13
Question 4 Multiple Choice (Single Answer)

What is the data type for the number 9.6352?

  1. float
  2. double
  3. Float
  4. Double
Question 5 Multiple Choice (Single Answer)

If result = 2 + 3 * 5, what is the value and type of 'result' variable?

  1. 17, byte
  2. 25, byte
  3. 17, int
  4. 25, int
Question 6 Multiple Choice (Single Answer)

Given the following code snippet;
int salaries[];
int index = 0;
salaries = new int salaries[4];
while (index < 4)
{
salaries[index] = 10000;
index++;
}
What is the value of salaries [3]?

  1. 40000
  2. 50000
  3. 15000
  4. 10000
Question 7 Multiple Choice (Single Answer)

How many numeric data types are supported in Java?

  1. 8
  2. 4
  3. 2
  4. 6
Question 8 Multiple Choice (Single Answer)

Assume that the value 3929.92 is of type 'float'. How to assign this value after declaring the variable 'interest' of type float?

  1. interest = 3929.92
  2. interest = (Float)3929.92
  3. interest = 3929.92 (float)
  4. interest = 3929.92f
Question 9 Multiple Choice (Single Answer)

All the wrapper classes (Integer, Boolean, Float, Short, Long, Double and Character) in java

  1. are private
  2. are serializable
  3. are immutatable
  4. are final
Question 10 Multiple Choice (Single Answer)

Which of the following statements is true?

  1. A super class is a sub set of a sub class
  2. class ClassTwo extends ClassOne means ClassOne is a subclass
  3. class ClassTwo extends ClassOne means ClassTow is a super class
  4. the class Class is the super class of all other classes in Java.
Question 11 Multiple Choice (Single Answer)

Which of the following statements is preferred to create a string "Welcome to Java Programming"?

  1. String str = “Welcome to Java Programming”
  2. String str = new String( “Welcome to Java Programming” )
  3. String str; str = “Welcome to Java Programming”
  4. String str; str = new String (“Welcome to Java Programming” )
Question 12 Multiple Choice (Single Answer)

Which of the following statements is true?

  1. The default char data type is a space( ' ' ) character.
  2. The default integer data type is 'int' and real data type is 'float'
  3. The default integer data type is 'long' and real data type is 'float'
  4. The default integer data type is 'int' and real data type is 'double'
Question 13 Multiple Choice (Single Answer)

Consider the following code snippet. What will be assigned to the variable fourthChar, if the code is executed?
String str = new String(“Java”);
char fourthChar = str.charAt(4);

  1. a
  2. v
  3. throws StringIndexOutofBoundsException
  4. null characater
Question 14 Multiple Choice (Single Answer)

What kind of thread is the Garbage collector thread is?

  1. Non daemon thread
  2. Daemon thread
  3. Thread with dead state
  4. None of the above
Question 15 Multiple Choice (Single Answer)

When a thread terminates its processing, into what state that thread enters?

  1. Running state
  2. Waiting state
  3. Dead state
  4. Beginning state
Question 16 Multiple Choice (Single Answer)

Which statement is true?

  1. HashTable is a sub class of Dictionary
  2. ArrayList is a sub class of Vector
  3. LinkedList is a subclass of ArrayList
  4. Vector is a subclass of Stack
Question 17 Multiple Choice (Single Answer)

Which of the following statements declare class Sample to belong to the payroll.admindept package?

  1. package payroll;package admindept;
  2. import payroll.*;
  3. package payroll.admindept.Sample;
  4. import payroll.admindept.*;
Question 18 Multiple Choice (Single Answer)

Which of these statements is true?

  1. LinkedList extends List
  2. AbstractSet extends Set
  3. HashSet extends AbstractSet
  4. WeakHashMap extends HashMap
Question 19 Multiple Choice (Single Answer)

The class java.lang.Exception is

  1. protected
  2. extends Throwable
  3. implements Throwable
  4. serializable
Question 20 Multiple Choice (Single Answer)

Which three guarantee that a thread will leave the running state?
yield()
wait()
notify()
notifyAll()
sleep(1000)
aLiveThread.join()
Thread.killThread()

  1. 1, 2 and 4
  2. 2, 5 and 6
  3. 3, 4 and 7
  4. 4, 5 and 7
Question 21 Multiple Choice (Single Answer)

What will be the output of the program?
class A
{
final public int GetResult(int a, int b) { return 0; }
}
class B extends A
{
public int GetResult(int a, int b) {return 1; }
}
public class Test
{
public static void main(String args[])
{
B b = new B();
System.out.println("x = " + b.GetResult(0, 1));
}
}

  1. x = 0
  2. x = 1
  3. Compilation fails.
  4. An exception is thrown at runtime.
Question 22 Multiple Choice (Single Answer)

The code snippet
if( "Welcome".trim() == "Welcome".trim() )
System.out.println("Equal");
else
System.out.println("Not Equal");
will

  1. compile and display “Equal”
  2. compile and display “Not Equal”
  3. cause a compiler error
  4. compile and display NULL
Question 23 Multiple Choice (Single Answer)

class HappyGarbage01
{
public static void main(String args[])
{
HappyGarbage01 h = new HappyGarbage01();
h.methodA(); /* Line 6 */
}
Object methodA()
{
Object obj1 = new Object();
Object [] obj2 = new Object[1];
obj2[0] = obj1;
obj1 = null;
return obj2[0];
}
}
Where will be the most chance of the garbage collector being invoked?

  1. After line 9
  2. After line 10
  3. After line 11
  4. Garbage collector never invoked in methodA()
Question 24 Multiple Choice (Single Answer)

What will be the output of the program?
class SC2
{
public static void main(String [] args)
{
SC2 s = new SC2();
s.start();
}

void start() 
{
    int a = 3;
    int b = 4;
    System.out.print(&quot; &quot; + 7 + 2 + &quot; &quot;);
    System.out.print(a + b);
    System.out.print(&quot; &quot; + a + b + &quot; &quot;);
    System.out.print(foo() + a + b + &quot; &quot;);
    System.out.println(a + b + foo());
}

String foo() 
{
    return &quot;foo&quot;;
}

}

  1. 9 7 7 foo 7 7foo
  2. 72 34 34 foo34 34foo
  3. 9 7 7 foo34 34foo
  4. 72 7 34 foo34 7foo
Question 25 Multiple Choice (Single Answer)

What will be the output of the program?
class BoolArray
{
boolean [] b = new boolean[3];
int count = 0;

void set(boolean [] x, int i) 
{
    x[i] = true;
    ++count;
}

public static void main(String [] args) 
{
    BoolArray ba = new BoolArray();
    ba.set(ba.b, 0);
    ba.set(ba.b, 2);
    ba.test();
}

void test() 
{
    if ( b[0] &amp;&amp; b[1] | b[2] )
        count++;
    if ( b[1] &amp;&amp; b[(++count - 2)] )
        count += 7;
    System.out.println(&quot;count = &quot; + count);
}

}

  1. count = 0
  2. count = 2
  3. count = 3
  4. count = 4