You are on page 1of 23

Consider the following string:

String hannah = "Did Hannah see bees? Hannah did."; Question: What is the value displayed by the expression hannah.length()?

Question: What is the value returned by the method call hannah.charAt(12)?

Question: Write an expression that refers to the letter b in the string referred to by hannah.

Question: How long is the string returned by the following expression? What is the string?

"Was it a car or a cat I saw?".substring(9, 12)

Exercise: Show two ways to concatenate the following two strings together to get the string "Hi, mom.":

String hi = "Hi, "; String mom = "mom.";

Based on the following code, how many objects are eligible for garbage collection at line #11:

1:import java.util.*; 2:public class test 3:{ 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: private static StringBuffer doTest () { Vector v = new Vector (); StringBuffer x = new StringBuffer ("DoTest"); v.add (x); return x; } } public static void main (String args[]) { StringBuffer a = new StringBuffer ("Hello"); StringBuffer b = new StringBuffer ("World"); StringBuffer c = new StringBuffer ("Hi World"); a = b; c = doTest ();

20:} A) 0 B) 1 C) 2 D) 3 E) 4

Consider the following code that contains the class definitions for class ABC, XYZ, and Alphabet. What will be the output of this code?

class ABC extends XYZ { ABC () { super (); System.out.print (" ABC "); } public static void main (String args[]) { XYZ x1 = new ABC (); } }

class XYZ extends Alphabet { XYZ () {

System.out.print (" XYZ "); } }

class Alphabet { void Alphabet () { System.out.print (" Alphabet "); } } A) XYZ ABC B) ABC XYZ C) Alphabet XYZ ABC D) ABC XYZ Alphabet E) ABC

Notice that the so-called constructor for class Alphabet is NOT really a constructor. A constructor must NOT have a return type:

void Alphabet () { System.out.print (" Alphabet "); }

Since this has a return type as void, this is considered a method and NOT a constructor. As a result, Alphabet will have a default no-arg constructor generated automatically by the compiler. Constructor execution is as follows:

1)ABC constructor is invoked 2) XYZ's constructor is invoked (XYZ is a superclass of ABC) 3) Alphabet's no-arg constructor is invoked (this is the default constructor generated by the compiler) 4) Object constructor is invoked 5)Object constructor completes 6) Alphabet no-arg default constructor completes (does NOT print anything since the constructor was generated by compiler) 7) XYZ constructor completes. (prints XYZ) 8) ABC constructor completes (prints ABC)

Consider the following code:

1:class test 2:{ 3: 4: 5: 6: 7: 8: 9: 10: 11:} 12:} What should the value of Z be in line #8 if the output of the code is "Count=} System.out.println ("Count=" + counter); public static void main (String args[]) { double array [] = { -10.000, 2.4, -4.0, 0.1, 3.2, -0.9}; int counter=0; for (int i=0; i < array.length; i++) { if (Math.floor (array[i] - 0.5) == Math.round (array[i] -Z)) counter--;

6"? A) 1 B) 0.5 C) 0 D) -1 E) None of the choices are correct

What is the output of the following code?

1:public class test 2:{ 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: public static void main (String args[]) { Thread t= new Thread (new Runnable () { public void run () { try { for (int i=1; i < 5; i++) { System.out.print (" " + i); wait (1000); } } catch (Exception ex) { ex.printStackTrace (); } } });

18: 19: 20:}

t.start (); }

A) A compilation error will occur B) 1 2 3 4 will be printed with a 1-second pause between each print out C) A run-time error will be thrown D) 1 2 3 4 will be printed with a 10-second pause between each print out E) None of the choices are correct

Which methods, when inserted at line # 3, will cause compilation to fail?

1: class ABC extends XYZ 2: { 3: //INSERT HERE 4: } 5: 6: class XYZ 7: { 8: int count; 9: 10: 11: 12: 13: 14: public int increment (int b) { count++; return count; }

15: 16: 17: 18: 19: 20:}

public void printCount() { System.out.print ("Count= " + count); }

A) public void print () { System.out.print ("Count = " + count); } B) public int increment (long b) throws java.io.IOException { return 1; } C) public void printCount(java.io.OutputStream os) throws java.io.IOException {} D) private int increment (int b) { return b+1; } E) public String printCount () { System.out.print ("Count= " + count); return count + ""; }

What is the output of the following code?

class test { public static void main (String args[])

{ int a=52; do while (a < 52) System.out.print ("a= " + a); while (a >= 52);

} }

A) The program terminates normally, but no output is printed B) The program is stuck in an infinite loop, but no output is printed C) a=52 D) a=52 is repeated in an infinite loop E) A compilation error occurs

class test { public static void main (String args[]) { int a=52; do while (a < 52) System.out.print ("a= " + a); while (a >= 52); } }

Notice that the code consists of a while loop nested inside a do-while loop. The syntax is perfectly legal and will compile without errors. However, since (a < 52) evaluates to false, the code will NOT print anything. However, since (a>= 52) evaluates to true, the outer-loop will NEVER terminate, and the program will be stuck in an infinite loop.

What is the output of the following code?

class test { public static void main (String args[])

{ int a=52; do while (a < 52) System.out.print ("a= " + a); while (a >= 52); } }

class test { public static void main (String args[]) { int a=52; do while (a < 52) System.out.print ("a= " + a); while (a >= 52); } }

Notice that the code consists of a while loop nested inside a do-while loop. The syntax is perfectly legal and will compile without errors. However, since

(a < 52) evaluates to false, the code will NOT print anything. However, since (a>= 52) evaluates to true, the outer-loop will NEVER terminate, and the program will be stuck in an infinite loop.

What are common methods between String and StringBuffer?

Select the common methods, which are defined for both type String and type StringBuffer ? (choose 3) toString() length() append(String) trim() equals(Object)

if(0.0 == -0.0) { System.out.println("true"); } else{ System.out.println("false"); }

prints false

prints true

class C { public static void main(String[] args) { double d1 = Math.floor(0.5); double d2 = Math.floor(1.5); System.out.print(d1 + "," + d2); }}

Prints: 0.0,1.0

Prints: 0.0,2.0

Prints: 1.0,1.0

Prints: 1.0,2.0

None of the above

Explanation: No Explanation Available

72)

System.out.println("String".substring(0,4)); This statement will Print

will print "Strin"

will print "Stri"

will cause compiler error

none of the above

if("String".replace('t','T') == "String".replace('t','T')) System.out.println("Equal"); else System.out.println("Not Equal");

will Print Equal

will Print Not Equal

compile time error

none of the above

Explanation: No Explanation Available

74)

Which of the following classes will not allow unsynchronized read operations by multiple threads?

Vector

TreeMap

TreeSet

HashMap

HashSet

Explanation: No Explanation Available

StringBuffer objects once created can not be modified

true

false

Explanation: No Explanation available

Given below the sample code :

public class Check{ public static void main(String args[]){ ArrayList alist = new ArrayList(); ArrayList<String> listStr = alist; ArrayList<StringBuffer> listBuf = alist; listStr.add(0, "Welcome"); StringBuffer buff = listBuf.get(0); System.out.println(buff.toString()); } }

What will be the output of the following?

1. Welcome 2. Compile error 3. java.lang.ClassCastException 4. null

Given below the sample code :

import java.util.LinkedList; import java.util.Queue; public class Check { public static void main(String... args) { Queue<String> que = new LinkedList<String>(); que.add("Delhi"); que.add("Mumbai"); que.add("Dhaka"); show(que); } public static void show(Queue q) { que.add(new Integer(12)); while (!que.isEmpty ( ) ) System.out.print(que.poll() + " "); }

What will be the output of the above code ?

1. Compile error

2. Delhi Mumbai Dhaka 12

3. Delhi Mumbai Dhaka

4. Delhi Mumbai

Given below the sample code :

public class Check{ public static void main(String... args) { ArrayList<Integer> alist = new ArrayList<Integer>(); alist.add(3); alist.add(12); alist.add(21); alist.add(30); for(int a : alist) System.out.println(a); }

What will be the output of the above code ?

1. compile error

2. 3 12 21 30

3. 0 0 0 0

4. 30 21 12 3

Given below the sample code :

public class Check{ public static void main(String... args) { ArrayList<Integer> alist = new ArrayList<Integer>(); alist.add(3); alist.add(12); alist.add(21); alist.add(30); for(int a : alist) System.out.println(a); } }

What will be the output of the above code ?

1. compile error

2. 3 12 21 30

3. 0 0 0 0

4. 30 21 12 3

Answer

(2) The sample example given below will test your understanding of EnumMap in Java. Given below the sample code :

11 EnumMap< Integer> frequencyEnumMap = new EnumMap< Integer>(ordinaryMap); 12 frequencyEnumMap.put(null, 100); 13 System.out.println("Frequency EnumMap: " + frequencyEnumMap);

What will be the output of the above code ?

1. compile error

2. 100

3. null

4. NullPointerException

The Sample code given below will test your knowledge of HashMap of Collection framework in Java. Given below the sample code :

4. HashMap hm = new HashMap(); 5. hm.put("key45", "some value"); 6. hm.put("key12", "some other value"); 7. hm.put("key39", "yet another value"); 8. Set a = hm.keySet(); 9. // insert code here

What code should be at line 9 to sort the keys in HashMap "hm"?

1. a = new SortedSet(a) ;

2. Collections.sort(a);

3. a = new TreeSet(a);

4. a = Collections.sort(a);

The Sample code given below test you understanding about the Collection Interface, ArrayList class and HashSet class of Collection framework. Given below the sample code :

import java.util.* public class Demo{ enum test{FIRST,SECOND,THIRD} public static void main(String args[]) { Collection myCol=new ArrayList(); myCol.add(test.THIRD); myCol.add(test.THIRD); myCol.add(test.THIRD); myCol.add(test.SECOND); myCol.add(test.SECOND); myCol.add(test.ONE); Set myset=new HashSet(mycol) }

Which statement given below is correct for the code above ?

1.The set contains all 6 elements of collection in the given order.

2.The set contains only 3 elements of collection in the given order.

3.The set contains all 6 elements of collection but not in the given order.

4.The set contains only 3 elements of collection but not in the given order.

(4)The Sample code will test your understanding about the Object class of Java. Given below the sample code :

33. Object [] Obj = { 34. new Integer(12), 35. new String("foo"), 36. new Integer(5), 37. new Boolean(true) 38. }; 39. Arrays.sort(Obj); 40. for(int i=0; i<Obj.length; i++) { 41. System.out.print(Obj[i].toString()); 42. System.out.print(" "); 43. }

What will be the output of the above code ?

1. ClassCastException at line 39.

2. Error at line 33

3. Error at line 41

4. 12 foo 5 true

The Sample code given below will test your understanding of Collection framework of Java. Given below the sample code :

1. import java.util.*; 2. public class Demo { 3. public static void main(String[] args) { 4. // insert code here 5. t.add(new Integer(2)); 6. t.add(new Integer(1)); 7. System.out.println(t); 8. } 9. }

You might also like