The sample test is modelled on the Sun Certification for Java 2 Programmer exam. The test has 59 questions and needs to be executed in 2 hours. The real exam may be a little tougher than this. The sample test is still in beta. So please let me know if you find any issues with the test.
- Which of the following are correct definitions of method main of a class. Select all correct answeres.
- public static int main(char args[]);
- public static void main(String args[]);
- public static void MAIN(String args[]);
- public static void main(String args);
- public static void main(char args[]);
- What all gets printed when the following code is compiled and run ? Select all correct answers.
public class xyz {
public static void main(String args[]) {
for(int i = 0; i < 2; i++) {
for(int j = 2; j>= 0; j•) {
if(i == j) break;
System.out.println("i=" + i + " j="+j);
}
}
}
}
- i=0 j=0
- i=0 j=1
- i=0 j=2
- i=1 j=0
- i=1 j=1
- i=1 j=2
- i=2 j=0
- i=2 j=1
- i=2 j=2
- What gets printed when the following code is compiled and run with the following arguments -
java test 2
Select the one correct answer.
public class test {
public static void main(String args[]) {
Integer intObj = Integer.valueOf(args[args.length - 1]);
int i = intObj.intValue();
if(args.length > 1)
System.out.println(i);
if(args.length > 0)
System.out.println(i - 1);
else
System.out.println(i - 2);
}
}
- test
- test -1
- 0
- 1
- 2
- In Java what expression can be used to represent number of elements in an array named arr ?
- How would the number 5 be represented in hex using upto four characters.
- Which of the following is a Java keyword. Select all correct answers.
- extern
- synchronized
- volatile
- friend
- friendly
- transient
- this
- then
- Is the following statement true or false. The constructor of a class must not have a return type.
- true
- false
- What is the number of bytes used by Java primitive long. Select the one correct answer.
- The number of bytes is compiler dependent.
- 2
- 4
- 8
- 64
- What is the result of invoking the method substring(2, 4) on the string "example"? Include the answer in quotes as the result is of type String.
- Which of the following is correct ? Select all correct answers.
- The native keyword indicates that the method is implemented in another language like C/C++.
- The only statements that can appear before an import statement in a Java file are comments.
- The method definitions inside interfaces are public and abstract. They cannot be private or protected.
- A class constructor may have public or protected keyord before them, nothing else.
- What is the result of evaluating the expression 14 ^ 23. Select the one correct answer.
- 25
- 37
- 6
- 31
- 17
- 9
- 24
- Which of the following are true. Select all correct answers.
- && operator is used for short-circuited logical AND.
- ~ operator is the bit-wise XOR operator.
- | operator is used to perform bitwise OR and also short-circuited logical OR.
- The unsigned right shift operator in Java is >>.
- Name the access modifier which when used with a method, makes it available to all the classes in the same package and to all the subclasses of the class.
- Which of the following is true. Select all correct answers.
- A class that is abstract may not be instantiated.
- The final keyword indicates that the body of a method is to be found elsewhere. The code is written in non-Java language, typicaly in C/C++.
- A static variable indicates there is only one copy of that variable.
- A method defined as private indicates that it is accessible to all other classes in the same package.
- What all gets printed when the following program is compiled and run. Select all correct answers.
public class test {
public static void main(String args[]) {
int i, j=1;
i = (j>1)?2:1;
switch(i) {
case 0: System.out.println(0); break;
case 1: System.out.println(1);
case 2: System.out.println(2); break;
case 3: System.out.println(3); break;
}
}
}
- 0
- 1
- 2
- 3
- What all gets printed when the following program is compiled and run. Select all correct answers.
public class test {
public static void main(String args[]) {
int i=0, j=2;
do {
i=++i;
j•;
} while(j>0);
System.out.println(i);
}
}
- 0
- 1
- 2
- The program does not compile because of statement "i=++i;"
- What all gets printed when the following gets compiled and run. Select all correct answers.
public class test {
public static void main(String args[]) {
int i=1, j=1;
try {
i++;
j•;
if(i/j > 1)
i++;
}
catch(ArithemticException e) {
System.out.println(0);
}
catch(ArrayIndexOutOfBoundsException e) {
System.out.println(1);
}
catch(Exception e) {
System.out.println(2);
}
finally {
System.out.println(3);
}
System.out.println(4);
}
}
- 0
- 1
- 2
- 3
- 4
- What all gets printed when the following gets compiled and run. Select all correct answer.
public class test {
public static void main(String args[]) {
int i=1, j=1;
try {
i++;
j•;
if(i == j)
i++;
}
catch(ArithmeticException e) {
System.out.println(0);
}
catch(ArrayIndexOutOfBoundsException e) {
System.out.println(1);
}
catch(Exception e) {
System.out.println(2);
}
finally {
System.out.println(3);
}
System.out.println(4);
}
}
- 0
- 1
- 2
- 3
- 4
- What all gets printed when the following gets compiled and run. Select all correct answer.
public class test {
public static void main(String args[]) {
String s1 = "abc";
String s2 = "abc";
if(s1 == s2)
System.out.println(1);
else
System.out.println(2);
if(s1.equals(s2))
System.out.println(3);
else
System.out.println(4);
}
}
- 1
- 2
- 3
- 4
- What all gets printed when the following gets compiled and run. Select all correct answer.
public class test {
public static void main(String args[]) {
String s1 = "abc";
String s2 = new String("abc");
if(s1 == s2)
System.out.println(1);
else
System.out.println(2);
if(s1.equals(s2))
System.out.println(3);
else
System.out.println(4);
}
}
- 1
- 2
- 3
- 4
- The default layout manager for a Frame is ...
- FlowLayout
- BorderLayout
- GridLayout
- GridBagLayout
- CardLayout
- Which of the following are valid adapter classes in Java. Select all correct answers.
- ComponentAdapter
- ActionAdapter
- AdjustmentAdapter
- ItemAdapter
- FocusAdapter
- Which of the following are legal array declarations. Select all correct answers.
- int i[5][];
- int i[][];
- int []i[];
- int i[5][5];
- int[][] a;
- What is the range of values that can be specified for an int. Select the one correct answer.
- The range of values is compiler dependent.
- -231 to 231 - 1
- -231-1 to 231
- -215 to 215 - 1
- -215-1 to 215
- How can you ensure that the memory allocated by an object is freed. Select the one correct answer.
- By invoking the free method on the object.
- By calling system.gc() method.
- By setting all references to the object to new values (say null).
- Garbage collection cannot be forced. The programmer cannot force the compiler to free the memory used by an object.
- What gets printed when the following code is compiled. Select the one correct answer.
public class test {
public static void main(String args[]) {
int i = 1;
do {
i•;
} while (i > 2);
System.out.println(i);
}
}
- 0
- 1
- 2
- -1
- Which of these is a legal definition of a method named m assuming it throws IOException, and returns void. Also assume that the method does not take any arguments. Select all correct answers.
- void m() throws IOException{}
- void m() throw IOException{}
- void m(void) throws IOException{}
- m() throws IOException{}
- void m() throws IOException
- void m() {} throws IOException
- Which of the following are legal identifier names in Java. Select all correct answers.
- %abcd
- $abcd
- 1abcd
- package
- _a_long_name
- At what stage in the following method does the string "abc" becomes available for garbage collection. Select the one correct answer.
void method X() {
String r = new String("abc");
String s = "abc";
r = r+1; //1
r = null; //2
s = s + r; //3
} //4
- Before statement labelled 1
- Before statement labelled 2
- Before statement labelled 3
- Before statement labelled 4
- Never.
-
String s = new String("xyz");
Assuming the above declaration, which of the following statements would compile. Select all correct answers.
- s = 2 * s;
- int i = s[0];
- s = s + s;
- s = s >> 2;
- None of the above.
- Which of the following statements related to Garbage Collection are correct. Select all correct answers.
- It is posible for a program to free memory at a given time.
- Garbage Collection feature of Java ensures that the program never runs out of memory.
- It is possible for a program to make an object available for Garbage Collection.
- The finalize method of an object is invoked before garbage collection is performed on the object.
- If a base class has a method defined as
void method() { }
Which of the following are legal prototypes in a derived class of this class. Select all correct answers.
- void method() { }
- int method() { return 0;}
- void method(int i) { }
- private void method() { }
- In which all cases does an exception gets generated. Select all correct answers.
int i = 0, j = 1;
- if((i == 0) || (j/i == 1))
- if((i == 0) | (j/i == 1))
- if((i != 0) && (j/i == 1))
- if((i != 0) & (j/i == 1))
- Which method defined in the EventObject class returns the Object that generated an event. The method should be given in the format - return_type method_name();
- Which of the following object receives ActionEvent. Select all the correct answers.
- List
- Button
- Choice
- CheckBox
- TextField
- MenuItem
- Name the class that may be used to create submenus in pull-down menus.
- Which of the following statements are true. Select all correct answers.
- The wait method defined in the Thread class, can be used to convert a thread from Running state to Waiting state.
- The wait(), notify(), notifyAll() must be executed in sybchronized code.
- The notify() method can be used to signal and move waiting threads to ready-to-run state.
- The Thread class is an abstract class.
- In which class is the wait() method defined. Select the one correct answer.
- Applet
- Runnable
- Thread
- Object
- Which keyword when applied on a method indicates that only one thread should execute the method at a time. Select the one correct answer.
- transient
- volatile
- synchronized
- native
- static
- final
- What is the name of the Collection interface used to represent elements in a sequence (in a particular order). Select the one correct answer.
- Collection
- Set
- List
- Map
- Which of these classes implement the Collection interface SortedMap. Select all correct answers.
- HashMap
- HashTable
- TreeMap
- HashSet
- TreeSet
- Vector
- Which is the only layout manager which always honours the size of a component. Select the one correct answers.
- FlowLayout
- GridLayout
- BorderLayout
- CardLayout
- GridBagLayout
- Which of the following are true about interfaces. Select all correct answers.
- Methods declared in interfaces are implicitly private.
- Variables declared in interfaces are implicitly public, static, and final.
- An interface can extend any number of interfaces.
- The keyword implements indicate that an interface inherits from another.
- Assume that class A extends class B, which extends class C. Also all the three classes implement the method test(). How can a method in class A invoke the test() method defined in class C. Select the one correct answers.
- test();
- super.test();
- super.super.test();
- ::test();
- C.test();
- It is not possible to invoke test() method defined in C from a method in A.
- What is the return type of method round(double d) defined in Math class.
- What gets written on the screen when the following program is compiled and run. Select the one right answer.
public class test {
public static void main(String args[]) {
int i;
float f = 2.3f;
double d = 2.7;
i = ((int)Math.ceil(f)) * ((int)Math.round(d));
System.out.println(i);
}
}
- 4
- 5
- 6
- 6.1
- 9
- Is the following statement true or false. As the toString method is defined in the Object class, System.out.println can be used to print any object.
- true
- false
- Which of these classes defined in java.io and used for file-handling are abstract. Select all correct answers.
- InputStream
- PrintStream
- Reader
- FileInputStream
- FileWriter
-
Which of these are valid Event Listener interfaces. Select all correct answer.
- MouseMotionListener
- WindowListener
- DialogListener
- PaintListener
- Name the collection interface used to represent collections that maintain unique elements.
- What is the result of compiling and running the following program.
public class test {
public static void main(String args[]) {
String str1="abc";
String str2="def";
String str3=str1.concat(str2);
str1.concat(str2);
System.out.println(str1);
}
}
- abc
- def
- abcabc
- abcdef
- defabc
- abcdefdef
- Select the one correct answer. The number of characters in an object of a class String is given by
- The member variable called size
- The member variable called length
- The method size() returns the number of characters.
- The method length() returns the number of characters.
- Select the one correct answer. Which method defined in Integer class can be used to convert an Integer object to primitive int type.
- valueOf
- intValue
- getInt
- getInteger
- Name the return type of method hashCode() defined in Object class, which is used to get the unique hash value of an Object.
- Which of the following are correct. Select all correct answers.
- An import statement, if defined, must be the first statement of the file.
- private members are accessible to all classes in the same package.
- An abstract class can be declared as final.
- Local variables cannot be declared as static.
- Name the keyword which makes a variable belong to a class, rather than being defined for each instance of the class. Select the one correct answer.
- static
- final
- abstract
- native
- volatile
- transient
- Which of these are core interfaces in the collection framework. Select all correct answers.
- Tree
- Stack
- Queue
- Array
- LinkedList
- Map
- Which abstract class is the superclass of all menu-related classes.
- Which of these statements are true. Select all correct answers.
- For each try block there must be at least one catch block defined.
- A try block may be followed by any number of finally blocks.
- A try block must be followed by at least one finally or catch block.
- If both catch and finally blocks are defined, catch block must precede the finally block.