Java Programming

Java Programming

Objects and Collections has 30 Questions

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?

TreeMap
HashMap
LinkedHashMap
The answer depends on the implementation of the existing instance.
What will be the output of the program?
public class Test 
{ 
    public static void main (String[] args) 
    {
        String foo = args[1]; 
        String bar = args[2]; 
        String baz = args[3]; 
        System.out.println("baz = " + baz); /* Line 8 */
    } 
}

And the command line invocation:


baz =
baz = null
baz = blue
Runtime Exception

which statement is true?
class Test1 
{
    public int value;
    public int hashCode() { return 42; }
}
class Test2 
{
    public int value;
    public int hashcode() { return (int)(value^5); }
}


class Test1 will not compile.
The Test1 hashCode() method is more efficient than the Test2 hashCode() method.
The Test1 hashCode() method is less efficient than the Test2 hashCode() method.
class Test2 will not compile.
Which class does not override the equals() and hashCode() methods, inheriting them directly from class Object?

java.lang.String
java.lang.Double
java.lang.StringBuffer
java.lang.Character
What will be the output of the program?
public class Test 
{ 
    public static void main (String args[]) 
    {
        String str = NULL; 
        System.out.println(str); 
    } 
}

NULL
Compile Error
Code runs but no output
Runtime Exception
Which statement is true for the class java.util.HashSet?

The elements in the collection are ordered.
The collection is guaranteed to be immutable.
The elements in the collection are guaranteed to be unique.
The elements in the collection are accessed using a unique key.
Which collection class allows you to grow or shrink its size and provides indexed access to its elements, but whose methods are not synchronized?

java.util.HashSet
java.util.LinkedHashSet
java.util.List
java.util.ArrayList
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 */
    } 
}

Compilation will succeed.
Compilation will fail at line 3.
Compilation will fail at line 5.
Compilation will fail at line 15.
Which of the following statements about the hashcode() method are incorrect?
  1. The value returned by hashcode() is used in some collection classes to help locate objects.
  2. The hashcode() method is required to return a positive int value.
  3. The hashcode() method in the String class is the one inherited from Object.
  4. Two new empty String objects will produce identical hashcodes.

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

Java.util.Map
Java.util.List
Java.util.Collection
None of the above
What will be the output of the program?
public class Test 
{ 
    private static int[] x; 
    public static void main(String[] args) 
    { 
        System.out.println(x[0]); 
    } 
}

0
null
Compile Error
NullPointException at runtime
What two statements are true about properly overridden hashCode() and equals() methods?
  1. hashCode() doesn't have to be overridden if equals() is.
  2. equals() doesn't have to be overridden if hashCode() is.
  3. hashCode() can always return the same value, regardless of the object that invoked it.
  4. equals() can be true even if it's comparing different objects.

1 and 2
2 and 3
3 and 4
1 and 3
You need to store elements in a collection that guarantees that no duplicates are stored and all elements can be accessed in natural order. Which interface provides that capability?

java.util.Map
java.util.Set
java.util.List
java.util.Collection
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); 
    } 
}

Prints: false, false, false
Prints: false, false, true
Prints: false, true, false
Prints: false, true, true
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.

1 and 4
2 and 3
3 and 4
1 and 3
Which interface does java.util.HashTable implement?

Java.util.Map
Java.util.List
Java.util.HashTable
Java.util.Collection
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]); 
    } 
}

f[0] = 0
f[0] = 0.0
Compile Error
Runtime Exception
System.out.println("x = " + x);and assuming that the equals() and hashCode() methods are property implemented, if the output is "x = 1111", which of the following statements will always be true?
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;


x2.equals(x1)
x3.hashCode() == x4.hashCode()
x5.hashCode() != x6.hashCode()
x8.equals(x7)
Which interface provides the capability to store objects using a key-value pair?

Java.util.Map
Java.util.Set
Java.util.List
Java.util.Collection
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); 
    } 
}

Prints: false,false,false
Prints: false,false,true
Prints: false,true,false
Prints: true,false,false
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.

2, 3, 4 and 5
1, 3, 4 and 5
3, 4 and 5
1, 2 and 3
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?

java.util.ArrayList
java.util.LinkedHashMap
java.util.HashMap
java.util.TreeMap
What will be the output of the program?
TreeSet map = new TreeSet();
map.add("one");
map.add("two");
map.add("three");
map.add("four");
map.add("one");
Iterator it = map.iterator();
while (it.hasNext() ) 
{
    System.out.print( it.next() + " " );
}

one two three four
four three two one
four one three two
one two three four one
Which statement is true for the class java.util.ArrayList?

The elements in the collection are ordered.
The collection is guaranteed to be immutable.
The elements in the collection are guaranteed to be unique.
The elements in the collection are accessed using a unique key.
Which collection class allows you to access its elements by associating a key with an element's value, and provides synchronization?

java.util.SortedMap
java.util.TreeMap
java.util.TreeSet
java.util.Hashtable
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()); 
}

42
Runtime Exception
Compile Error at line 2
Compile Error at line 5
Which is valid declaration of a float?

float f = 1F;
float f = 1.0;
float f = "1";
float f = 1.0d;

What line of code should replace the missing statement to make this program compile?
/* 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"); 
    } 
}


No statement required.
import java.io.*;
include java.io.*;
import java.io.PrintWriter;
What is the numerical range of char?

0 to 32767
0 to 65535
-256 to 255
-32768 to 32767
Which of the following are Java reserved words?
  1. run
  2. import
  3. default
  4. implement

1 and 2
2 and 3
3 and 4
2 and 4