1]Which declaration of the main method below would allow a class to be started as a standalone program. Select the one correct answer.
a) public static int main(char args[])
b) public static void main(String args[])
c) public static void MAIN(String args[])
d) public static void main(String args)
e) public static void main(char args[])
What all gets printed when the following code is compiled and run? Select the three 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);
} } } }
a) i=0 j=0
b) i=0 j=1
c) i=0 j=2
d) i=1 j=0
e) i=1 j=1
f) i=1 j=2
g) i=2 j=0
h) i=2 j=1
i) i=2 j=2
What gets printed when the following code is compiled and run with the following command -
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);
}}
a) test
b) test -1
c) 0
d) 1
e) 2
In Java technology 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 up-to four characters.
Which of the following is a Java keyword. Select the four correct answers.
a) extern
b) synchronized
c) volatile
d) friend
e) friendly
f) transient
g) this
h) then
Is the following statement true or false. The constructor of a class must not have a return type.
a) true b)false
What is the number of bytes used by Java primitive long. Select the one correct answer.
a) The number of bytes is compiler dependent.
b) 2
c) 4
d) 8
e) 64
What is returned when the method substring(2, 4) is invoked on the string "example"? Include the answer in quotes as the result is of type String.
Which of the following is correct? Select the two correct answers.
a) The native keyword indicates that the method is implemented in another language like C/C++.
b) The only statements that can appear before an import statement in a Java file are comments.
c) The method definitions inside interfaces are public and abstract. They cannot be private or protected.
d) A class constructor may have public or protected keyword before them, nothing else.
What is the result of evaluating the expression 14 ^ 23. Select the one correct answer.
a) 25 37 6 31 17 9 24
11] Which of the following are true. Select the one correct answers.
a) && operator is used for short-circuited logical AND.
b) ~ operator is the bit-wise XOR operator.
c) | operator is used to perform bitwise OR and also short-circuited logical OR.
d) 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 the two correct answers.
a) A class that is abstract may not be instantiated.
b) The final keyword indicates that the body of a method is to be found elsewhere. The code is written in non-Java language, typically in C/C++.
c) A static variable indicates there is only one copy of that variable.
d) A method defined as private indicates that it is accessible to all other classes in the same package.
14] What all gets printed when the following program is compiled and run. Select the two 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;
} } }
a) 0 1 2 3
15] What all gets printed when the following program is compiled and run. Select the one correct answer.
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);
}}
a) 0 1 2 The program does not compile because of statement "i=++i;"
16]What all gets printed when the following gets compiled and run. Select the three 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(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);
} }
a) 0 1 2 3 4
17] What all gets printed when the following gets compiled and run. Select the two correct answers.
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);
} }
a) 0 1 2 3 4
What all gets printed when the following gets compiled and run. Select the two correct answers.
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);
} }
a) 1
b) 2
c) 3
d) 4
19]What all gets printed when the following gets compiled and run. Select the two correct answers.
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);
} }
a) 1
b) 2
c) 3
d) 4
20] Which of the following are legal array declarations. Select the three correct answers.
int i[5][];
int i[][];
int []i[];
int i[5][5];
int[][] a;
21]What is the range of values that can be specified for an int. Select the one correct answer.
a) The range of values is compiler dependent.
b) -231 to 231 - 1
c) -231-1 to 231
d) -215 to 215 - 1
e) -215-1 to 215
How can you ensure that the memory allocated by an object is freed. Select the one correct answer.
a) By invoking the free method on the object.
b) By calling system.gc() method.
c) By setting all references to the object to new values (say null).
d) Garbage collection cannot be forced. The programmer cannot force the JVM to free the memory used by an object.
23]What gets printed when the following code is compiled and run. 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);
}
}
a) 0
b) 1
c) 2
d) -1
24]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 the one correct answer.
a) void m() throws IOException{}
b) void m() throw IOException{}
c) void m(void) throws IOException{}
d) m() throws IOException{}
e) void m() {} throws IOException
25] Which of the following are legal identifier names in Java. Select the two correct answers.
a) %abcd
b) $abcd
c) 1abcd
d) package
e) _a_long_name
At what stage in the following method does the object initially referenced by s becomes available for garbage collection. Select the one correct answer.
void method X() {
String r = new String("abc");
String s = new String("abc");
r = r+1; //1
r = null; //2
s = s + r; //3
} //4
a) Before statement labeled 1
b) Before statement labeled 2
c) Before statement labeled 3
d) Before statement labeled 4
e) Never.
27]String s = new String("xyz"); Assuming the above declaration, which of the following statements would compile. Select the one correct answer.
a) s = 2 * s;
b) int i = s[0];
c) s = s + s;
d) s = s >> 2;
e) None of the above.
28]Which of the following statements related to Garbage Collection are correct. Select the two correct
a) It is possible for a program to free memory at a given time.
b) Garbage Collection feature of Java ensures that the program never runs out of memory.
c) It is possible for a program to make an object available for Garbage Collection.
d) The finalize method of an object is invoked before garbage collection is performed on the object.
29]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 the two correct answers.
a) void method() { }
b) int method() { return 0;}
c) void method(int i) { }
d) private void method() { }
30] In which all cases does an exception gets generated. Select the two correct answers.
a) int i = 0, j = 1;
b) if((i == 0) || (j/i == 1))
c) if((i == 0) | (j/i == 1))
d) if((i != 0) && (j/i == 1))
e) if((i != 0) & (j/i == 1))
31] Which of the following statements are true. Select the two correct answers.
a) The wait method defined in the Thread class, can be used to convert a thread from Running state to Waiting state.
b) The wait(), notify(), and notifyAll() methods must be executed in synchronized code.
c) The notify() and notifyAll() methods can be used to signal and move waiting threads to ready-to-run state.
d) The Thread class is an abstract class.
Which keyword when applied on a method indicates that only one thread should execute the method at a time. Select the one correct answer.
a) transient
b) volatile
c) synchronized
d) native
e) static
f) 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.
a) Collection
b) Set
c) List
d) Map
34]Which of these classes implement the Collection interface SortedMap. Select the one correct answers.
a) HashMap
b) Hashtable
c) TreeMap
d) HashSet
e) TreeSet
f) Vector
35]Which of the following are true about interfaces. Select the two correct answers.
a) Methods declared in interfaces are implicitly private.
b) Variables declared in interfaces are implicitly public, static, and final.
c) An interface can extend any number of interfaces.
d) 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 a class A invoke the test() method defined in class C (without creating a new instance of class C). Select the one correct answer.
a) test();
b) super.test();
c) super.super.test();
d) ::test();
e) C.test();
f) It is not possible to invoke test() method defined in C from a method in A.
37]What is the return type of method round(double d) defined in Math class.
38]What gets written on the screen when the following program is compiled and run. Select one right ans
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);
} }
a) 4
b) 5
c) 6
d) 6.1
e) 9
39]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.
a) true false
40] Which of these classes defined in java.io and used for file-handling are abstract. Select two correct ans a) InputStream
b) PrintStream
c) Reader
d) FileInputStream
e) FileWriter
41]Name the collection interface used to represent collections that maintain unique elements.
42]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);
}
}
a) abc
b) def
c) abcabc
d) abcdef
e) defabc
f) abcdefdef
43]Select the one correct answer. The number of characters in an object of a class String is given by
a) The member variable called size
b) The member variable called length
c) The method size() returns the number of characters.
d) 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.
a) valueOf
b) intValue
c) getInt
d) getInteger
45]Name the return type of method hashCode() defined in Object class, which is used to get the unique hash value of an Object.
46]Which of the following are correct. Select the one correct answer.
a) An import statement, if defined, must always be the first non-comment statement of the file.
b) private members are accessible to all classes in the same package.
c) An abstract class can be declared as final.
d) Local variables cannot be declared as static.
Name the keyword that makes a variable belong to a class, rather than being defined for each instance of the class. Select the one correct answer.
a) static
b) final
c) abstract
d) native
e) volatile
f) transient
48]Which of these are core interfaces in the collection framework. Select the one correct answer.
a) Tree
b) Stack
c) Queue
d) Array
e) LinkedList
f) Map
Which of these statements are true. Select the two correct answers.
a) For each try block there must be at least one catch block defined.
b) A try block may be followed by any number of finally blocks.
c) A try block must be followed by at least one finally or catch block.
d) If both catch and finally blocks are defined, catch block must precede the finally block.
==================================================================================================
The remaining questions are related to AWT, event classes, and layout managers. These topics are not included in 1.4 version of the exam.
The default layout manager for a Frame is ...
a) FlowLayout
b) BorderLayout
c) GridLayout
d) GridBagLayout
e) CardLayout
51]Which of the following are valid adapter classes in Java. Select the two correct answers.
a) ComponentAdapter
b) ActionAdapter
c) AdjustmentAdapter
d) ItemAdapter
e) FocusAdapter
52]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 the four correct answers.
a) List
b) Button
c) Choice
d) CheckBox
e) TextField
f) MenuItem
54]Name the class that may be used to create submenus in pull-down menus.
55]In which class is the wait() method defined. Select the one correct answer.
a) Applet
b) Runnable
c) Thread
d) Object
Which is the only layout manager that always honors the size of a component. Select the one correct answer.
a) FlowLayout
b) GridLayout
c) BorderLayout
d) CardLayout
e) GridBagLayout
Which of these are valid Event Listener interfaces. Select the two correct answers.
a) MouseMotionListener
b) WindowListener
c) DialogListener
d) PaintListener
58]Which abstract class is the super class of all menu-related classes.
Answers to Sample Test 1
1) b
2) b, c, f
3) d. Note that the program gets one command line argument - 2. args.length will get set to 1. So the condition if(args.length > 1) will fail, and the second check if(args.length > 0) will return true.
4) arr.length
5) 4]Any of these is correct - 0x5, 0x05, 0X05, 0X5
6) b, c, f, g
7) a
8) d
9) "am"
10) a, c. Please note that b is not correct. A package statement may appear before an import statement. A class constructor may be declared private also. Hence d is incorrect.
11) a
12) a
13) 12]protected
14) a, c
15) b, c
16) c
17) a, d, e
18) d, e
19) 18]a, c
20) b, c
21) 20]b, c, e
22) b
23) d
24) 23]a
25) a
26) b, e . The option c is incorrect because a Java identifier name cannot begin with a digit.
27) 26]d
28) c
29) c, d
30) a, c
31) 30]b, d
32) b, c
33) c
34) c
35) c
36) 35]b, c
37) f
38) long
39) e
40) a
41) a, c
42) 41]Set
43) a
44) d
45) b
46) 45]int
47) d
48) a
49) f
50) c, d
51) 50]b
52) a, e
53) Object getSource();
54) a, b, e, f
55) Menu
56) d
57) a
58) a, b
59) MenuComponent
Mock Exam 2
Which of the following are Java keywords? Select the three correct answers.
a) external
b) implement
c) throw
d) void
e) integer
f) private
g) synchronize
h) unsigned
Which of the following are legal definitions of the main method that can be used to execute a class. Select the one correct answer.
a) public void main(String args)
b) public static int main(String args[])
c) public static void main(String args[])
d) static public void MAIN(String args[])
e) public static void main(string args[])
f) public static void main(String *args)
Which of these are legal array declarations or definitions? Select the two correct answers.
a) int[] []x[];
b) int *x;
c) int x[5];
d) int[] x = {1,2,3};
Name the collection interface used to represent a sequence of numbers in a fixed order.
The class Hashtable is used to implement which collection interface. Select the one correct answer.
a) Table
b) List
c) Set
d) SortedSet
e) Map
What gets printed when the following program is compiled and run? Select the one correct answer.
class test {
public static void main(String args[]) {
int i;
do {
i++;
}
while(i < 0);
System.out.println(i);
}
}
a) The program does not compile as i is not initialized.
b) The program compiles but does not run.
c) The program compiles and runs but does not print anything.
d) The program prints 0.
e) The program prints 1.
What gets printed when the following program is compiled and run? Select the one correct answer.
class xyz {
static int i;
public static void main(String args[]) {
while (i < 0) {
i--;
}
System.out.println(i);
}
}
a) The program does not compile as i is not initialized.
b) The program compiles but does not run.
c) The program compiles and runs but does not print anything.
d) The program prints 0.
e) The program prints 1.
What gets printed when the following program is compiled and run? Select the one correct answer.
class xyz {
public static void main(String args[]) {
int i,j,k;
for (i = 0; i < 3; i++) {
for(j=1; j < 4; j++) {
for(k=2; k<5; k++) {
if((i == j) && (j==k))
System.out.println(i);
}
}
}
}
}
a) 0
b) 1
c) 2
d) 3
e) 4
Using up to four characters what is the Java representation of the number 23 in hex?
What gets printed when the following program is compiled and run? Select the one correct answer.
class test {
static boolean check;
public static void main(String args[]) {
int i;
if(check == true)
i=1;
else
i=2;
if(i=2) i=i+2;
else i = i + 4;
System.out.println(i);
}
}
a) 3
b) 4
c) 5
d) 6
e) The program does not compile because of the statement if(i=2)
Select the one correct answer. The smallest number that can be represented using short primitive type in Java is -
a) 0
b) -127
c) -128
d) -16384
e) -32768
f) The smallest number is compiler dependent.
Given the following declarations, which of the assignments given in the options below would compile. Select the two correct answers.
a) int i = 5;
b) boolean t = true;
c) float f = 2.3F;
d) double d = 2.3;
e) t = (boolean) i;
f) f = d;
g) d = i;
h) i = 5;
i) f = 2.8;
What gets printed when the following program is compiled and run. Select the one correct answer.
public class incr {
public static void main(String args[]) {
int i , j;
i = j = 3;
int n = 2 * ++i;
int m = 2 * j++;
System.out.println(i + " " + j + " " + n + " " + m);
}
}
a) 4 4 8 6
b) 4 4 8 8
c) 4 4 6 6
d) 4 3 8 6
e) 4 3 8 8
f) 4 4 6 8
Given two non-negative integers a and b and a String str, what is the number of characters in the expression str.substring(a,b) . Select the one correct answer.
a) a + b
b) a - b
c) b - a - 1
d) b - a + 1
e) b - a
f) b
What is the result of compiling and running the following program. Select the one correct answer.
class test {
public static void main(String args[]) {
char ch;
String test2 = "abcd";
String test = new String("abcd");
if(test.equals(test2)) {
if(test == test2)
ch = test.charAt(0);
else
ch = test.charAt(1);
}
else {
if(test == test2)
ch = test.charAt(2);
else
ch = test.charAt(3);
}
System.out.println(ch);
}
}
a) 'a'
b) 'b'
c) 'c'
d) 'd'
What is the result of compiling and running the following program. Select the one correct answer.
class test {
public static void main(String args[]) {
int i,j=0;
for(i=10;i<0;i--) { j++; }
switch(j) {
case (0) :
j=j+1;
case(1):
j=j+2;
break;
case (2) :
j=j+3;
break;
case (10) :
j=j+10;
break;
default :
break;
}
System.out.println(j);
}
}
a) 0
b) 1
c) 2
d) 3
e) 10
f) 20
What is the number displayed when the following program is compiled and run.
class test {
public static void main(String args[]) {
test test1 = new test();
System.out.println(test1.xyz(100));
}
public int xyz(int num) {
if(num == 1) return 1;
else return(xyz(num-1) + num);
}
}
Which of the following statements are true. Select the one correct answer.
a) Arrays in Java are essentially objects.
b) It is not possible to assign one array to another. Individual elements of array can however be assigned.
c) Array elements are indexed from 1 to size of array.
d) If a method tries to access an array element beyond its range, a compile warning is generated.
Which expression can be used to access the last element of an array. Select the one correct answer.
a) array[array.length()]
b) array[array.length() - 1]
c) array[array.length]
d) array[array.length - 1]
What is the result of compiling and running the following program. Select the one correct answer.
class test {
public static void main(String args[]) {
int[] arr = {1,2,3,4};
call_array(arr[0], arr);
System.out.println(arr[0] + "," + arr[1]);
}
static void call_array(int i, int arr[]) {
arr[i] = 6;
i = 5;
}
}
a) 1,2
b) 5,2
c) 1,6
d) 5,6
Which of the following statements are correct. Select the one correct answer.
a) Each Java file must have exactly one package statement to specify where the class is stored.
b) If a Java file has both import and package statement, the import statement must come before package statement.
c) A Java file has at least one class defined.
d) If a Java file has a package statement, it must be the first statement (except comments).
What happens when the following program is compiled and then the command "java check it out" is executed. Select the one correct answer.
class check {
public static void main(String args[]) {
System.out.println(args[args.length-2]);
}
}
a) The program does not compile.
b) The program compiles but generates ArrayIndexOutOfBoundsException exception.
c) The program prints java
d) The program prints check
e) The program prints it
f) The program prints out
What all gets printed when the following code is compiled and run. Select the three correct answers.
class test {
public static void main(String args[]) {
int i[] = {0,1};
try {
i[2] = i[0] + i[1];
}
catch(ArrayIndexOutOfBoundsException e1) {
System.out.println("1");
}
catch(Exception e2) {
System.out.println("2");
}
finally {
System.out.println(3);
}
System.out.println("4");
}
}
a) 1
b) 2
c) 3
d) 4
A program needs to store the name, salary, and age of employees in years. Which of the following data types should be used to create the Employee class. Select the three correct answers.
a) char
b) boolean
c) Boolean
d) String
e) int
f) double
To make a variable defined in a class accessible only to methods defined in the classes in same package, which of the following keyword should be used. Select the one correct answer.
a) By using the keyword package before the variable.
b) By using the keyword private before the variable.
c) By using the keyword protected before the variable.
d) By using the keyword public before the variable.
e) The variable should not be preceded by any of the above mentioned keywords.
In implementing two classes Employee and Manager, such that each Manager is an Employee, what should be the relationship between these classes. Select the one correct answer.
a) Employee should be the base class of Manager class.
b) Manager should be the base class of Employee class.
c) Manager class should include the Employee class as a data member.
d) Employee class should include Manager class as a data member.
e) The Manager and Employee should not have any relationship.
Select the one most appropriate answer. What is the purpose of method parseInt defined in Integer class.
a) The method converts an integer to a String.
b) The method is used to convert String to an integer, assuming that the String represents an integer.
c) The method is used to convert String to Integer class, assuming that the String represents an integer.
d) The method converts the Integer object to a String.
What should be done to invoke the run() method on a thread for an object derived from the Thread class. Select the one correct answer.
a) The run() method should be directly invoked on the Object.
b) The start() method should be directly invoked on the Object.
c) The init() method should be directly invoked on the Object.
d) The creation of the object using the new operator would create a new thread and invoke its run() method.
What is the default priority of a newly created thread.
a) MIN_PRIORITY (which is defined as 1 in the Thread class.)
b) NORM_PRIORITY (which is defined as 5 in the Thread class.)
c) MAX_PRIORITY (which is defined as 10 in the Thread class.)
d) A thread inherits the priority of its parent thread.
The remaining questions are from AWT and related topics, and are not relevant for SCJP 1.4 .
Which of following correctly describes the functionality of the method drawRect(int a, int b, int c, int d) defined in jawa.awt.Graphics class. Select the one correct option.
a) Draws the outline of a rectangle with a, b being the x,y co-ordinates of top left corner, and c,d being the x,y co-ordinates of the bottom right corner.
b) Draws the outline of a rectangle with a, b being the x,y co-ordinates of top left corner, and c,d being the width and height of the rectangle.
c) Draws a filled rectangle with a, b being the x,y co-ordinates of top left corner, and c,d being the x,y co-ordinates of the bottom right corner.
d) Draws a filled rectangle with a, b being the x,y co-ordinates of top left corner, and c,d being the width and height of the rectangle.
Which Listener interface must be implemented by a class responsible for handling mouse clicks on buttons?
The getSource method defined in the EventObject class returns the source of an event. What is the return type of this getSource method?
a) EventObject
b) Event
c) Object
d) Component
e) Button
The focusLost method is defined in FocusListener interface and is executed when a control loses focus. What is the argument of focusLost method?
Which of the following is the super class of these classes - ContainterEvent, FocusEvent, InputEvent, PaintEvent, WindowEvent. Select the one correct answer.
a) ActionEvent
b) AdjustmentEvent
c) ComponentEvent
d) ItemEvent
e) TextEvent
f) Event
Which of these are adapter classes. Select the three correct answers.
a) ComponentAdapter
b) ItemAdapter
c) ActionAdapter
d) KeyAdapter
e) ContainerAdapter
Which of the following statements about layout managers is true. Select the one correct answer.
a) FlowLayout places components left-aligned in a row (by default) and when there is no space in a row, another row is started.
b) FlowLayout provides a constructor Flowlayout(int align, int x, int y), where x and y are the coordinates of the first component being added.
c) The FlowLayout is the default layout manager for Window class
d) Default horizontal and vertical gaps of components placed using FlowLayout is 5 pixels.
Which of the following is true about BorderLayout. Select the two correct answers.
a) The default layout manager for Applet class is BorderLayout.
b) BorderLayout places components in North, South, East and West first and then the remaining space is occupied by the Center component.
c) When a component is added in BorderLayout using the add method, it is placed in the center by default.
d) The BorderLayout always honors the size of components provided by the program.
Which of these is true about the GridBagLayout. Select the one correct answer.
a) The weightx and weighty fields of GridBagConstraints specify how many column and rows each component occupies.
b) The gridwidth and gridheight constraints of GridBagConstraints specify the width and height in pixels of each cell.
c) GridBagLayout is the default layout manager of the Frame class.
d) The gridx and gridy parameters of GridBagConstraints define the column and row position of the upper left corner of the component.
Answers to Sample Test 2
1) c, d, f
2) c. The main method must be static and return void. Hence a and b are incorrect. It must take an array of String as argument. Hence e and f are incorrect. As Java is case sensitive, d is incorrect.
3) a, d
4) List
5) e. The collection interface Map has two implementation HashMap and Hashtable.
6) Local variables are not initialized by default. They must be initialized before they are used.
7) d. The variable i gets initialized to zero. The while loop does not get executed.
8) During various iterations of three loops, the only time i, j and k have same values are when all of them are set to 2.
9) 0x17 or 0X17.
10) e. The statement "i=2" evaluates to 2. The expression within the if block must evaluate to a boolean.
11) e. The range of short primitive type is -32768 to 32767.
12) c,d. Java does not allow casts between boolean values and any numeric types. Hence a is incorrect. Assigning double to a float requires an explicit cast. Hence b and e are incorrect.
13) a
14) e
15) Both Strings test and test2 contain "abcd" . They are however located at different memory addresses. Hence test == test2 returns false, and test.equals(test2) returns true.
16) The for loop does not get executed even once as the condition (i < 0) fails in the first iteration. In the switch statement, the statement j = j +1; gets executed, setting j to 1. As there is no break after this case, the next statement also gets executed setting j to 3.
17) 5050. The recursive function xyz essentially sums up numbers 1 to num. This evaluates to (num * (num + 1))/2.
18) Java supports assignment of one array to another. Hence b is incorrect. Array elements are indexed from 0. Hence c is incorrect. A method that accesses array elements out of its range does not generate a compilation error. Hence d is incorrect.
19) array.length gives the number of elements in the array. As indexes in Java start from 0, d is the correct answer.
20) In the invocation of call_array, the first element is invoked using call-by-value, and the second using call-by-reference.
21) import statement, package statement and class definitions are all optional in a file. Hence a and c are incorrect. If both import and package statements are present in a file, then package statement must appear before the import statement. Hence b is incorrect.
22) The args array consists of two elements "it" and "out". args.length is set to two.
23) a,c,d. The exception ArrayIndexOutOfBoundsException is generated as the main method tries to access i[2]. Hence 1 gets printed. After this finally block gets excuted, before the program exits.
24) d,e,f
25) A data member that does not have public/protected/private is accessible to all methods in the same package.
26) The Manager and Employee share as "is a" relationship - A Manager is an Employee. This is captured by making Employee the base class of Manager.
27) The method int parseInt(Sting s) returns the integer value corresponding to input String, assuming that the input string represents an integer in base 10.
28) The start() method invokes the run() method when the thread is ready to execute.
29) d
30) b. drawRect method draws the outline of a rectangle. The last two arguments are width and height of the rectangle.
31) ActionListener
32) The getSource method returns a reference to the object where the event initially occurred.
33) FocusEvent. A class implementing FocusListener interface must implement the following method -
34) public void focusLost(FocusEvent)
35) public void focusGained(FocusEvent)
36) c
37) a,d,e. There are no adapter classes corresponding to the following interfaces - ActionListener, ItemListener, AdjustmentListener, and TextListener.
38) The default alignment for FlowLayout is CENTER. Hence a is incorrect. The default Layout Manager for Window class is BorderLayout. Hence c is incorrect. X and y in option b indicate horizontal and vertical gaps between components.
39) b,c. The default Layout Manager for Applet class is FlowLayout. Hence a is incorrect. BorderLayout grows all components to fill the space available. Hence d is incorrect.
40) d. The weightx and weighty specify how the size of a cell should change when the container exceeds the preferred size of component. Hence a is not correct. gridwidth and gridheight specify how many columns and rows the component specifies. So b is incorrect. BorderLayout is the default layout manager for Frame class.
home | tutorial | questions | test 1 Questions on Classes
What gets displayed on the screen when the following program is compiled and run. Select the one correct answer.
protected class example {
public static void main(String args[]) {
String test = "abc";
test = test + test;
System.out.println(test);
}
}
a) The class does not compile because the top level class cannot be protected.
b) The program prints "abc"
c) The program prints "abcabc"
d) The program does not compile because statement "test = test + test" is illegal.
A top level class may have only the following access modifier. Select the one correct answer.
a) package
b) friendly
c) private
d) protected
e) public
Write down the modifier of a method that makes the method available to all classes in the same package and to all the subclasses of this class.
Select the one most appropriate answer. A top level class without any modifier is accessible to -
a) any class
b) any class within the same package
c) any class within the same file
d) any subclass of this class.
Is this True or False. In Java an abstract class cannot be sub-classed.
Is this True or False. In Java a final class must be sub-classed before it can be used.
Which of the following are true. Select the three correct answers.
a) A static method may be invoked before even a single instance of the class is constructed.
b) A static method cannot access non-static methods of the class.
c) Abstract modifier can appear before a class or a method but not before a variable.
d) final modifier can appear before a class or a variable but not before a method.
e) Synchronized modifier may appear before a method or a variable but not before a class.
Answers to questions on classes in Java
1) a
2) e
3) protected
4) b
5) False
6) False
7) a, b, c. final modifier may appear before a method, a variable or before a class.
Questions on AWT
This topic is part of SCJP 1.2 exam but not SCJP 1.4 exam.
Which of the following classes are derived from the Container class. Select the four correct answers.
a) Component
b) Panel
c) java.applet.Applet
d) Dialog
e) Frame
f) MenuComponent
Which of the following classes are derived from the Component class. Select the four correct answers.
a) Container
b) Window
c) List
d) MenuItem
e) Choice
Name the class used to represent a GUI application window, which is optionally resizable and can have a title bar, an icon, and menus. Select the one correct answer.
a) Window
b) Panel
c) Dialog
d) Frame
Which abstract class is the super class of all menu related classes.
Which of these classes can be added to a Frame component. Select the three correct answers.
a) Menu
b) Button
c) PopupMenu
d) Window
e) List
Which class can be used to represent a checkbox with a textual label that can appear in a menu. Select the one correct answer.
a) MenuBar
b) MenuItem
c) CheckboxMenuItem
d) Menu
e) CheckBox
Which of these classes can be added to any Container class, using the add method defined in Container class. Select the two correct answers.
a) Button
b) CheckboxMenuItem
c) Menu
d) Canvas
Answers to questions on AWT
1) b, c, d, e
2) a, b, c, e
3) d
4) MenuComponent
5) b, c, e
6) c
7) a, d
Questions on Collections
TreeMap class is used to implement which collection interface. Select the one correct answer.
a) Set
b) SortedSet
c) List
d) Tree
e) SortedMap
Name the Collection interface implemented by the Vector class.
Name the Collection interface implemented by the Hashtable class.
Name the Collection interface implemented by the HashSet class.
Which of these are interfaces in the collection framework. Select the two correct answers.
a) Set
b) List
c) Array
d) Vector
e) LinkedList
Which of these are interfaces in the collection framework. Select the two correct answers.
a) HashMap
b) ArrayList
c) Collection
d) SortedMap
e) TreeMap
What is the name of collection interface used to maintain non-unique elements in order.
What is the name of collection interface used to maintain unique elements.
What is the name of collection interface used to maintain mappings of keys to values.
Is this true or false. Map interface is derived from the Collection interface.
a) True
b) False
Answers to questions on Collections
1) e
2) List
3) Map
4) Set
5) a,b
6) c,d
7) List
8) Set
9) Map
10) b
11) Advertisement
March Mini Quiz
New to Java Programming Center
What is the intial layout manager of a JApplet?
A. FlowLayout
B. OverlayLayout
C. GridLayout
D. BorderLayout
E. AppletLayout
If you add a component to the content pane of a JApplet and don't specify any constraints, what happens to it?
A. The add method rejects the operation.
B. The add method adds the component to the container, but doesn't place it in any of the areas of the content pane.
C. The add method adds the component to the Center area of the pane.
D. There is no way to call add without any constraints.
Where can you place a JMenuBar in a Swing applet?
A. Top of applet
B. Left side of applet
C. Right side of applet
D. Bottom of applet
E. Any of the above
Should speed keys be used in applet menus?
A. Yes
B. No
C. It depends
Do I have to use the HTML Converter to add a Swing applet on my web page?
A. Yes
B. No
Java Technology Fundaments Newsletters
March Quiz Answers
(March 2003)
1. What is the intial layout manager of a JApplet?
A is incorrect Answer (D): Even though you don't add components directly to a Swing applet, instead adding them to the content pane, the default layout manager of a JApplet really is BorderLayout.
2. If you add a component to the content pane of a JApplet and don't specify any constraints, what happens to it?
A is incorrect Answer (C): When there are no constraints specified, a JApplet will place added components into the center area of the BorderLayout for its content pane.
3. Where can you place a JMenuBar in a Swing applet?
B is incorrect Answer (E): While the setJMenuBar method will add a menu to the top of an applet, since JMenuBar is a component, it can be added anywhere that a component can be used.
4. Should speed keys be used in applet menus?
C is correct! Answer (C): While Swing permits menu items to have mnemonics associated with them, their use should probably be avoided, except where their usage is obvious. Speed keys only work when an applet has input focus. Thus, it may be confusing to the user to sometimes activate the menu associated with an applet and other times activate the menu associated wit the browser.
5. Do I have to use the HTML Converter to add a Swing applet on my web page?
A is incorrect Answer (B): Early versions of the Java Plugin required a tool called the HTML Converter to mangle the HTML that loaded a web page with the APPLET tag. Thankfully, that tool is no longer needed and you can use the APPLET tag directly, without the mangling.
a) public static int main(char args[])
b) public static void main(String args[])
c) public static void MAIN(String args[])
d) public static void main(String args)
e) public static void main(char args[])
What all gets printed when the following code is compiled and run? Select the three 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);
} } } }
a) i=0 j=0
b) i=0 j=1
c) i=0 j=2
d) i=1 j=0
e) i=1 j=1
f) i=1 j=2
g) i=2 j=0
h) i=2 j=1
i) i=2 j=2
What gets printed when the following code is compiled and run with the following command -
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);
}}
a) test
b) test -1
c) 0
d) 1
e) 2
In Java technology 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 up-to four characters.
Which of the following is a Java keyword. Select the four correct answers.
a) extern
b) synchronized
c) volatile
d) friend
e) friendly
f) transient
g) this
h) then
Is the following statement true or false. The constructor of a class must not have a return type.
a) true b)false
What is the number of bytes used by Java primitive long. Select the one correct answer.
a) The number of bytes is compiler dependent.
b) 2
c) 4
d) 8
e) 64
What is returned when the method substring(2, 4) is invoked on the string "example"? Include the answer in quotes as the result is of type String.
Which of the following is correct? Select the two correct answers.
a) The native keyword indicates that the method is implemented in another language like C/C++.
b) The only statements that can appear before an import statement in a Java file are comments.
c) The method definitions inside interfaces are public and abstract. They cannot be private or protected.
d) A class constructor may have public or protected keyword before them, nothing else.
What is the result of evaluating the expression 14 ^ 23. Select the one correct answer.
a) 25 37 6 31 17 9 24
11] Which of the following are true. Select the one correct answers.
a) && operator is used for short-circuited logical AND.
b) ~ operator is the bit-wise XOR operator.
c) | operator is used to perform bitwise OR and also short-circuited logical OR.
d) 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 the two correct answers.
a) A class that is abstract may not be instantiated.
b) The final keyword indicates that the body of a method is to be found elsewhere. The code is written in non-Java language, typically in C/C++.
c) A static variable indicates there is only one copy of that variable.
d) A method defined as private indicates that it is accessible to all other classes in the same package.
14] What all gets printed when the following program is compiled and run. Select the two 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;
} } }
a) 0 1 2 3
15] What all gets printed when the following program is compiled and run. Select the one correct answer.
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);
}}
a) 0 1 2 The program does not compile because of statement "i=++i;"
16]What all gets printed when the following gets compiled and run. Select the three 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(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);
} }
a) 0 1 2 3 4
17] What all gets printed when the following gets compiled and run. Select the two correct answers.
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);
} }
a) 0 1 2 3 4
What all gets printed when the following gets compiled and run. Select the two correct answers.
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);
} }
a) 1
b) 2
c) 3
d) 4
19]What all gets printed when the following gets compiled and run. Select the two correct answers.
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);
} }
a) 1
b) 2
c) 3
d) 4
20] Which of the following are legal array declarations. Select the three correct answers.
int i[5][];
int i[][];
int []i[];
int i[5][5];
int[][] a;
21]What is the range of values that can be specified for an int. Select the one correct answer.
a) The range of values is compiler dependent.
b) -231 to 231 - 1
c) -231-1 to 231
d) -215 to 215 - 1
e) -215-1 to 215
How can you ensure that the memory allocated by an object is freed. Select the one correct answer.
a) By invoking the free method on the object.
b) By calling system.gc() method.
c) By setting all references to the object to new values (say null).
d) Garbage collection cannot be forced. The programmer cannot force the JVM to free the memory used by an object.
23]What gets printed when the following code is compiled and run. 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);
}
}
a) 0
b) 1
c) 2
d) -1
24]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 the one correct answer.
a) void m() throws IOException{}
b) void m() throw IOException{}
c) void m(void) throws IOException{}
d) m() throws IOException{}
e) void m() {} throws IOException
25] Which of the following are legal identifier names in Java. Select the two correct answers.
a) %abcd
b) $abcd
c) 1abcd
d) package
e) _a_long_name
At what stage in the following method does the object initially referenced by s becomes available for garbage collection. Select the one correct answer.
void method X() {
String r = new String("abc");
String s = new String("abc");
r = r+1; //1
r = null; //2
s = s + r; //3
} //4
a) Before statement labeled 1
b) Before statement labeled 2
c) Before statement labeled 3
d) Before statement labeled 4
e) Never.
27]String s = new String("xyz"); Assuming the above declaration, which of the following statements would compile. Select the one correct answer.
a) s = 2 * s;
b) int i = s[0];
c) s = s + s;
d) s = s >> 2;
e) None of the above.
28]Which of the following statements related to Garbage Collection are correct. Select the two correct
a) It is possible for a program to free memory at a given time.
b) Garbage Collection feature of Java ensures that the program never runs out of memory.
c) It is possible for a program to make an object available for Garbage Collection.
d) The finalize method of an object is invoked before garbage collection is performed on the object.
29]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 the two correct answers.
a) void method() { }
b) int method() { return 0;}
c) void method(int i) { }
d) private void method() { }
30] In which all cases does an exception gets generated. Select the two correct answers.
a) int i = 0, j = 1;
b) if((i == 0) || (j/i == 1))
c) if((i == 0) | (j/i == 1))
d) if((i != 0) && (j/i == 1))
e) if((i != 0) & (j/i == 1))
31] Which of the following statements are true. Select the two correct answers.
a) The wait method defined in the Thread class, can be used to convert a thread from Running state to Waiting state.
b) The wait(), notify(), and notifyAll() methods must be executed in synchronized code.
c) The notify() and notifyAll() methods can be used to signal and move waiting threads to ready-to-run state.
d) The Thread class is an abstract class.
Which keyword when applied on a method indicates that only one thread should execute the method at a time. Select the one correct answer.
a) transient
b) volatile
c) synchronized
d) native
e) static
f) 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.
a) Collection
b) Set
c) List
d) Map
34]Which of these classes implement the Collection interface SortedMap. Select the one correct answers.
a) HashMap
b) Hashtable
c) TreeMap
d) HashSet
e) TreeSet
f) Vector
35]Which of the following are true about interfaces. Select the two correct answers.
a) Methods declared in interfaces are implicitly private.
b) Variables declared in interfaces are implicitly public, static, and final.
c) An interface can extend any number of interfaces.
d) 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 a class A invoke the test() method defined in class C (without creating a new instance of class C). Select the one correct answer.
a) test();
b) super.test();
c) super.super.test();
d) ::test();
e) C.test();
f) It is not possible to invoke test() method defined in C from a method in A.
37]What is the return type of method round(double d) defined in Math class.
38]What gets written on the screen when the following program is compiled and run. Select one right ans
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);
} }
a) 4
b) 5
c) 6
d) 6.1
e) 9
39]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.
a) true false
40] Which of these classes defined in java.io and used for file-handling are abstract. Select two correct ans a) InputStream
b) PrintStream
c) Reader
d) FileInputStream
e) FileWriter
41]Name the collection interface used to represent collections that maintain unique elements.
42]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);
}
}
a) abc
b) def
c) abcabc
d) abcdef
e) defabc
f) abcdefdef
43]Select the one correct answer. The number of characters in an object of a class String is given by
a) The member variable called size
b) The member variable called length
c) The method size() returns the number of characters.
d) 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.
a) valueOf
b) intValue
c) getInt
d) getInteger
45]Name the return type of method hashCode() defined in Object class, which is used to get the unique hash value of an Object.
46]Which of the following are correct. Select the one correct answer.
a) An import statement, if defined, must always be the first non-comment statement of the file.
b) private members are accessible to all classes in the same package.
c) An abstract class can be declared as final.
d) Local variables cannot be declared as static.
Name the keyword that makes a variable belong to a class, rather than being defined for each instance of the class. Select the one correct answer.
a) static
b) final
c) abstract
d) native
e) volatile
f) transient
48]Which of these are core interfaces in the collection framework. Select the one correct answer.
a) Tree
b) Stack
c) Queue
d) Array
e) LinkedList
f) Map
Which of these statements are true. Select the two correct answers.
a) For each try block there must be at least one catch block defined.
b) A try block may be followed by any number of finally blocks.
c) A try block must be followed by at least one finally or catch block.
d) If both catch and finally blocks are defined, catch block must precede the finally block.
==================================================================================================
The remaining questions are related to AWT, event classes, and layout managers. These topics are not included in 1.4 version of the exam.
The default layout manager for a Frame is ...
a) FlowLayout
b) BorderLayout
c) GridLayout
d) GridBagLayout
e) CardLayout
51]Which of the following are valid adapter classes in Java. Select the two correct answers.
a) ComponentAdapter
b) ActionAdapter
c) AdjustmentAdapter
d) ItemAdapter
e) FocusAdapter
52]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 the four correct answers.
a) List
b) Button
c) Choice
d) CheckBox
e) TextField
f) MenuItem
54]Name the class that may be used to create submenus in pull-down menus.
55]In which class is the wait() method defined. Select the one correct answer.
a) Applet
b) Runnable
c) Thread
d) Object
Which is the only layout manager that always honors the size of a component. Select the one correct answer.
a) FlowLayout
b) GridLayout
c) BorderLayout
d) CardLayout
e) GridBagLayout
Which of these are valid Event Listener interfaces. Select the two correct answers.
a) MouseMotionListener
b) WindowListener
c) DialogListener
d) PaintListener
58]Which abstract class is the super class of all menu-related classes.
Answers to Sample Test 1
1) b
2) b, c, f
3) d. Note that the program gets one command line argument - 2. args.length will get set to 1. So the condition if(args.length > 1) will fail, and the second check if(args.length > 0) will return true.
4) arr.length
5) 4]Any of these is correct - 0x5, 0x05, 0X05, 0X5
6) b, c, f, g
7) a
8) d
9) "am"
10) a, c. Please note that b is not correct. A package statement may appear before an import statement. A class constructor may be declared private also. Hence d is incorrect.
11) a
12) a
13) 12]protected
14) a, c
15) b, c
16) c
17) a, d, e
18) d, e
19) 18]a, c
20) b, c
21) 20]b, c, e
22) b
23) d
24) 23]a
25) a
26) b, e . The option c is incorrect because a Java identifier name cannot begin with a digit.
27) 26]d
28) c
29) c, d
30) a, c
31) 30]b, d
32) b, c
33) c
34) c
35) c
36) 35]b, c
37) f
38) long
39) e
40) a
41) a, c
42) 41]Set
43) a
44) d
45) b
46) 45]int
47) d
48) a
49) f
50) c, d
51) 50]b
52) a, e
53) Object getSource();
54) a, b, e, f
55) Menu
56) d
57) a
58) a, b
59) MenuComponent
Mock Exam 2
Which of the following are Java keywords? Select the three correct answers.
a) external
b) implement
c) throw
d) void
e) integer
f) private
g) synchronize
h) unsigned
Which of the following are legal definitions of the main method that can be used to execute a class. Select the one correct answer.
a) public void main(String args)
b) public static int main(String args[])
c) public static void main(String args[])
d) static public void MAIN(String args[])
e) public static void main(string args[])
f) public static void main(String *args)
Which of these are legal array declarations or definitions? Select the two correct answers.
a) int[] []x[];
b) int *x;
c) int x[5];
d) int[] x = {1,2,3};
Name the collection interface used to represent a sequence of numbers in a fixed order.
The class Hashtable is used to implement which collection interface. Select the one correct answer.
a) Table
b) List
c) Set
d) SortedSet
e) Map
What gets printed when the following program is compiled and run? Select the one correct answer.
class test {
public static void main(String args[]) {
int i;
do {
i++;
}
while(i < 0);
System.out.println(i);
}
}
a) The program does not compile as i is not initialized.
b) The program compiles but does not run.
c) The program compiles and runs but does not print anything.
d) The program prints 0.
e) The program prints 1.
What gets printed when the following program is compiled and run? Select the one correct answer.
class xyz {
static int i;
public static void main(String args[]) {
while (i < 0) {
i--;
}
System.out.println(i);
}
}
a) The program does not compile as i is not initialized.
b) The program compiles but does not run.
c) The program compiles and runs but does not print anything.
d) The program prints 0.
e) The program prints 1.
What gets printed when the following program is compiled and run? Select the one correct answer.
class xyz {
public static void main(String args[]) {
int i,j,k;
for (i = 0; i < 3; i++) {
for(j=1; j < 4; j++) {
for(k=2; k<5; k++) {
if((i == j) && (j==k))
System.out.println(i);
}
}
}
}
}
a) 0
b) 1
c) 2
d) 3
e) 4
Using up to four characters what is the Java representation of the number 23 in hex?
What gets printed when the following program is compiled and run? Select the one correct answer.
class test {
static boolean check;
public static void main(String args[]) {
int i;
if(check == true)
i=1;
else
i=2;
if(i=2) i=i+2;
else i = i + 4;
System.out.println(i);
}
}
a) 3
b) 4
c) 5
d) 6
e) The program does not compile because of the statement if(i=2)
Select the one correct answer. The smallest number that can be represented using short primitive type in Java is -
a) 0
b) -127
c) -128
d) -16384
e) -32768
f) The smallest number is compiler dependent.
Given the following declarations, which of the assignments given in the options below would compile. Select the two correct answers.
a) int i = 5;
b) boolean t = true;
c) float f = 2.3F;
d) double d = 2.3;
e) t = (boolean) i;
f) f = d;
g) d = i;
h) i = 5;
i) f = 2.8;
What gets printed when the following program is compiled and run. Select the one correct answer.
public class incr {
public static void main(String args[]) {
int i , j;
i = j = 3;
int n = 2 * ++i;
int m = 2 * j++;
System.out.println(i + " " + j + " " + n + " " + m);
}
}
a) 4 4 8 6
b) 4 4 8 8
c) 4 4 6 6
d) 4 3 8 6
e) 4 3 8 8
f) 4 4 6 8
Given two non-negative integers a and b and a String str, what is the number of characters in the expression str.substring(a,b) . Select the one correct answer.
a) a + b
b) a - b
c) b - a - 1
d) b - a + 1
e) b - a
f) b
What is the result of compiling and running the following program. Select the one correct answer.
class test {
public static void main(String args[]) {
char ch;
String test2 = "abcd";
String test = new String("abcd");
if(test.equals(test2)) {
if(test == test2)
ch = test.charAt(0);
else
ch = test.charAt(1);
}
else {
if(test == test2)
ch = test.charAt(2);
else
ch = test.charAt(3);
}
System.out.println(ch);
}
}
a) 'a'
b) 'b'
c) 'c'
d) 'd'
What is the result of compiling and running the following program. Select the one correct answer.
class test {
public static void main(String args[]) {
int i,j=0;
for(i=10;i<0;i--) { j++; }
switch(j) {
case (0) :
j=j+1;
case(1):
j=j+2;
break;
case (2) :
j=j+3;
break;
case (10) :
j=j+10;
break;
default :
break;
}
System.out.println(j);
}
}
a) 0
b) 1
c) 2
d) 3
e) 10
f) 20
What is the number displayed when the following program is compiled and run.
class test {
public static void main(String args[]) {
test test1 = new test();
System.out.println(test1.xyz(100));
}
public int xyz(int num) {
if(num == 1) return 1;
else return(xyz(num-1) + num);
}
}
Which of the following statements are true. Select the one correct answer.
a) Arrays in Java are essentially objects.
b) It is not possible to assign one array to another. Individual elements of array can however be assigned.
c) Array elements are indexed from 1 to size of array.
d) If a method tries to access an array element beyond its range, a compile warning is generated.
Which expression can be used to access the last element of an array. Select the one correct answer.
a) array[array.length()]
b) array[array.length() - 1]
c) array[array.length]
d) array[array.length - 1]
What is the result of compiling and running the following program. Select the one correct answer.
class test {
public static void main(String args[]) {
int[] arr = {1,2,3,4};
call_array(arr[0], arr);
System.out.println(arr[0] + "," + arr[1]);
}
static void call_array(int i, int arr[]) {
arr[i] = 6;
i = 5;
}
}
a) 1,2
b) 5,2
c) 1,6
d) 5,6
Which of the following statements are correct. Select the one correct answer.
a) Each Java file must have exactly one package statement to specify where the class is stored.
b) If a Java file has both import and package statement, the import statement must come before package statement.
c) A Java file has at least one class defined.
d) If a Java file has a package statement, it must be the first statement (except comments).
What happens when the following program is compiled and then the command "java check it out" is executed. Select the one correct answer.
class check {
public static void main(String args[]) {
System.out.println(args[args.length-2]);
}
}
a) The program does not compile.
b) The program compiles but generates ArrayIndexOutOfBoundsException exception.
c) The program prints java
d) The program prints check
e) The program prints it
f) The program prints out
What all gets printed when the following code is compiled and run. Select the three correct answers.
class test {
public static void main(String args[]) {
int i[] = {0,1};
try {
i[2] = i[0] + i[1];
}
catch(ArrayIndexOutOfBoundsException e1) {
System.out.println("1");
}
catch(Exception e2) {
System.out.println("2");
}
finally {
System.out.println(3);
}
System.out.println("4");
}
}
a) 1
b) 2
c) 3
d) 4
A program needs to store the name, salary, and age of employees in years. Which of the following data types should be used to create the Employee class. Select the three correct answers.
a) char
b) boolean
c) Boolean
d) String
e) int
f) double
To make a variable defined in a class accessible only to methods defined in the classes in same package, which of the following keyword should be used. Select the one correct answer.
a) By using the keyword package before the variable.
b) By using the keyword private before the variable.
c) By using the keyword protected before the variable.
d) By using the keyword public before the variable.
e) The variable should not be preceded by any of the above mentioned keywords.
In implementing two classes Employee and Manager, such that each Manager is an Employee, what should be the relationship between these classes. Select the one correct answer.
a) Employee should be the base class of Manager class.
b) Manager should be the base class of Employee class.
c) Manager class should include the Employee class as a data member.
d) Employee class should include Manager class as a data member.
e) The Manager and Employee should not have any relationship.
Select the one most appropriate answer. What is the purpose of method parseInt defined in Integer class.
a) The method converts an integer to a String.
b) The method is used to convert String to an integer, assuming that the String represents an integer.
c) The method is used to convert String to Integer class, assuming that the String represents an integer.
d) The method converts the Integer object to a String.
What should be done to invoke the run() method on a thread for an object derived from the Thread class. Select the one correct answer.
a) The run() method should be directly invoked on the Object.
b) The start() method should be directly invoked on the Object.
c) The init() method should be directly invoked on the Object.
d) The creation of the object using the new operator would create a new thread and invoke its run() method.
What is the default priority of a newly created thread.
a) MIN_PRIORITY (which is defined as 1 in the Thread class.)
b) NORM_PRIORITY (which is defined as 5 in the Thread class.)
c) MAX_PRIORITY (which is defined as 10 in the Thread class.)
d) A thread inherits the priority of its parent thread.
The remaining questions are from AWT and related topics, and are not relevant for SCJP 1.4 .
Which of following correctly describes the functionality of the method drawRect(int a, int b, int c, int d) defined in jawa.awt.Graphics class. Select the one correct option.
a) Draws the outline of a rectangle with a, b being the x,y co-ordinates of top left corner, and c,d being the x,y co-ordinates of the bottom right corner.
b) Draws the outline of a rectangle with a, b being the x,y co-ordinates of top left corner, and c,d being the width and height of the rectangle.
c) Draws a filled rectangle with a, b being the x,y co-ordinates of top left corner, and c,d being the x,y co-ordinates of the bottom right corner.
d) Draws a filled rectangle with a, b being the x,y co-ordinates of top left corner, and c,d being the width and height of the rectangle.
Which Listener interface must be implemented by a class responsible for handling mouse clicks on buttons?
The getSource method defined in the EventObject class returns the source of an event. What is the return type of this getSource method?
a) EventObject
b) Event
c) Object
d) Component
e) Button
The focusLost method is defined in FocusListener interface and is executed when a control loses focus. What is the argument of focusLost method?
Which of the following is the super class of these classes - ContainterEvent, FocusEvent, InputEvent, PaintEvent, WindowEvent. Select the one correct answer.
a) ActionEvent
b) AdjustmentEvent
c) ComponentEvent
d) ItemEvent
e) TextEvent
f) Event
Which of these are adapter classes. Select the three correct answers.
a) ComponentAdapter
b) ItemAdapter
c) ActionAdapter
d) KeyAdapter
e) ContainerAdapter
Which of the following statements about layout managers is true. Select the one correct answer.
a) FlowLayout places components left-aligned in a row (by default) and when there is no space in a row, another row is started.
b) FlowLayout provides a constructor Flowlayout(int align, int x, int y), where x and y are the coordinates of the first component being added.
c) The FlowLayout is the default layout manager for Window class
d) Default horizontal and vertical gaps of components placed using FlowLayout is 5 pixels.
Which of the following is true about BorderLayout. Select the two correct answers.
a) The default layout manager for Applet class is BorderLayout.
b) BorderLayout places components in North, South, East and West first and then the remaining space is occupied by the Center component.
c) When a component is added in BorderLayout using the add method, it is placed in the center by default.
d) The BorderLayout always honors the size of components provided by the program.
Which of these is true about the GridBagLayout. Select the one correct answer.
a) The weightx and weighty fields of GridBagConstraints specify how many column and rows each component occupies.
b) The gridwidth and gridheight constraints of GridBagConstraints specify the width and height in pixels of each cell.
c) GridBagLayout is the default layout manager of the Frame class.
d) The gridx and gridy parameters of GridBagConstraints define the column and row position of the upper left corner of the component.
Answers to Sample Test 2
1) c, d, f
2) c. The main method must be static and return void. Hence a and b are incorrect. It must take an array of String as argument. Hence e and f are incorrect. As Java is case sensitive, d is incorrect.
3) a, d
4) List
5) e. The collection interface Map has two implementation HashMap and Hashtable.
6) Local variables are not initialized by default. They must be initialized before they are used.
7) d. The variable i gets initialized to zero. The while loop does not get executed.
8) During various iterations of three loops, the only time i, j and k have same values are when all of them are set to 2.
9) 0x17 or 0X17.
10) e. The statement "i=2" evaluates to 2. The expression within the if block must evaluate to a boolean.
11) e. The range of short primitive type is -32768 to 32767.
12) c,d. Java does not allow casts between boolean values and any numeric types. Hence a is incorrect. Assigning double to a float requires an explicit cast. Hence b and e are incorrect.
13) a
14) e
15) Both Strings test and test2 contain "abcd" . They are however located at different memory addresses. Hence test == test2 returns false, and test.equals(test2) returns true.
16) The for loop does not get executed even once as the condition (i < 0) fails in the first iteration. In the switch statement, the statement j = j +1; gets executed, setting j to 1. As there is no break after this case, the next statement also gets executed setting j to 3.
17) 5050. The recursive function xyz essentially sums up numbers 1 to num. This evaluates to (num * (num + 1))/2.
18) Java supports assignment of one array to another. Hence b is incorrect. Array elements are indexed from 0. Hence c is incorrect. A method that accesses array elements out of its range does not generate a compilation error. Hence d is incorrect.
19) array.length gives the number of elements in the array. As indexes in Java start from 0, d is the correct answer.
20) In the invocation of call_array, the first element is invoked using call-by-value, and the second using call-by-reference.
21) import statement, package statement and class definitions are all optional in a file. Hence a and c are incorrect. If both import and package statements are present in a file, then package statement must appear before the import statement. Hence b is incorrect.
22) The args array consists of two elements "it" and "out". args.length is set to two.
23) a,c,d. The exception ArrayIndexOutOfBoundsException is generated as the main method tries to access i[2]. Hence 1 gets printed. After this finally block gets excuted, before the program exits.
24) d,e,f
25) A data member that does not have public/protected/private is accessible to all methods in the same package.
26) The Manager and Employee share as "is a" relationship - A Manager is an Employee. This is captured by making Employee the base class of Manager.
27) The method int parseInt(Sting s) returns the integer value corresponding to input String, assuming that the input string represents an integer in base 10.
28) The start() method invokes the run() method when the thread is ready to execute.
29) d
30) b. drawRect method draws the outline of a rectangle. The last two arguments are width and height of the rectangle.
31) ActionListener
32) The getSource method returns a reference to the object where the event initially occurred.
33) FocusEvent. A class implementing FocusListener interface must implement the following method -
34) public void focusLost(FocusEvent)
35) public void focusGained(FocusEvent)
36) c
37) a,d,e. There are no adapter classes corresponding to the following interfaces - ActionListener, ItemListener, AdjustmentListener, and TextListener.
38) The default alignment for FlowLayout is CENTER. Hence a is incorrect. The default Layout Manager for Window class is BorderLayout. Hence c is incorrect. X and y in option b indicate horizontal and vertical gaps between components.
39) b,c. The default Layout Manager for Applet class is FlowLayout. Hence a is incorrect. BorderLayout grows all components to fill the space available. Hence d is incorrect.
40) d. The weightx and weighty specify how the size of a cell should change when the container exceeds the preferred size of component. Hence a is not correct. gridwidth and gridheight specify how many columns and rows the component specifies. So b is incorrect. BorderLayout is the default layout manager for Frame class.
home | tutorial | questions | test 1 Questions on Classes
What gets displayed on the screen when the following program is compiled and run. Select the one correct answer.
protected class example {
public static void main(String args[]) {
String test = "abc";
test = test + test;
System.out.println(test);
}
}
a) The class does not compile because the top level class cannot be protected.
b) The program prints "abc"
c) The program prints "abcabc"
d) The program does not compile because statement "test = test + test" is illegal.
A top level class may have only the following access modifier. Select the one correct answer.
a) package
b) friendly
c) private
d) protected
e) public
Write down the modifier of a method that makes the method available to all classes in the same package and to all the subclasses of this class.
Select the one most appropriate answer. A top level class without any modifier is accessible to -
a) any class
b) any class within the same package
c) any class within the same file
d) any subclass of this class.
Is this True or False. In Java an abstract class cannot be sub-classed.
Is this True or False. In Java a final class must be sub-classed before it can be used.
Which of the following are true. Select the three correct answers.
a) A static method may be invoked before even a single instance of the class is constructed.
b) A static method cannot access non-static methods of the class.
c) Abstract modifier can appear before a class or a method but not before a variable.
d) final modifier can appear before a class or a variable but not before a method.
e) Synchronized modifier may appear before a method or a variable but not before a class.
Answers to questions on classes in Java
1) a
2) e
3) protected
4) b
5) False
6) False
7) a, b, c. final modifier may appear before a method, a variable or before a class.
Questions on AWT
This topic is part of SCJP 1.2 exam but not SCJP 1.4 exam.
Which of the following classes are derived from the Container class. Select the four correct answers.
a) Component
b) Panel
c) java.applet.Applet
d) Dialog
e) Frame
f) MenuComponent
Which of the following classes are derived from the Component class. Select the four correct answers.
a) Container
b) Window
c) List
d) MenuItem
e) Choice
Name the class used to represent a GUI application window, which is optionally resizable and can have a title bar, an icon, and menus. Select the one correct answer.
a) Window
b) Panel
c) Dialog
d) Frame
Which abstract class is the super class of all menu related classes.
Which of these classes can be added to a Frame component. Select the three correct answers.
a) Menu
b) Button
c) PopupMenu
d) Window
e) List
Which class can be used to represent a checkbox with a textual label that can appear in a menu. Select the one correct answer.
a) MenuBar
b) MenuItem
c) CheckboxMenuItem
d) Menu
e) CheckBox
Which of these classes can be added to any Container class, using the add method defined in Container class. Select the two correct answers.
a) Button
b) CheckboxMenuItem
c) Menu
d) Canvas
Answers to questions on AWT
1) b, c, d, e
2) a, b, c, e
3) d
4) MenuComponent
5) b, c, e
6) c
7) a, d
Questions on Collections
TreeMap class is used to implement which collection interface. Select the one correct answer.
a) Set
b) SortedSet
c) List
d) Tree
e) SortedMap
Name the Collection interface implemented by the Vector class.
Name the Collection interface implemented by the Hashtable class.
Name the Collection interface implemented by the HashSet class.
Which of these are interfaces in the collection framework. Select the two correct answers.
a) Set
b) List
c) Array
d) Vector
e) LinkedList
Which of these are interfaces in the collection framework. Select the two correct answers.
a) HashMap
b) ArrayList
c) Collection
d) SortedMap
e) TreeMap
What is the name of collection interface used to maintain non-unique elements in order.
What is the name of collection interface used to maintain unique elements.
What is the name of collection interface used to maintain mappings of keys to values.
Is this true or false. Map interface is derived from the Collection interface.
a) True
b) False
Answers to questions on Collections
1) e
2) List
3) Map
4) Set
5) a,b
6) c,d
7) List
8) Set
9) Map
10) b
11) Advertisement
March Mini Quiz
New to Java Programming Center
What is the intial layout manager of a JApplet?
A. FlowLayout
B. OverlayLayout
C. GridLayout
D. BorderLayout
E. AppletLayout
If you add a component to the content pane of a JApplet and don't specify any constraints, what happens to it?
A. The add method rejects the operation.
B. The add method adds the component to the container, but doesn't place it in any of the areas of the content pane.
C. The add method adds the component to the Center area of the pane.
D. There is no way to call add without any constraints.
Where can you place a JMenuBar in a Swing applet?
A. Top of applet
B. Left side of applet
C. Right side of applet
D. Bottom of applet
E. Any of the above
Should speed keys be used in applet menus?
A. Yes
B. No
C. It depends
Do I have to use the HTML Converter to add a Swing applet on my web page?
A. Yes
B. No
Java Technology Fundaments Newsletters
March Quiz Answers
(March 2003)
1. What is the intial layout manager of a JApplet?
A is incorrect Answer (D): Even though you don't add components directly to a Swing applet, instead adding them to the content pane, the default layout manager of a JApplet really is BorderLayout.
2. If you add a component to the content pane of a JApplet and don't specify any constraints, what happens to it?
A is incorrect Answer (C): When there are no constraints specified, a JApplet will place added components into the center area of the BorderLayout for its content pane.
3. Where can you place a JMenuBar in a Swing applet?
B is incorrect Answer (E): While the setJMenuBar method will add a menu to the top of an applet, since JMenuBar is a component, it can be added anywhere that a component can be used.
4. Should speed keys be used in applet menus?
C is correct! Answer (C): While Swing permits menu items to have mnemonics associated with them, their use should probably be avoided, except where their usage is obvious. Speed keys only work when an applet has input focus. Thus, it may be confusing to the user to sometimes activate the menu associated with an applet and other times activate the menu associated wit the browser.
5. Do I have to use the HTML Converter to add a Swing applet on my web page?
A is incorrect Answer (B): Early versions of the Java Plugin required a tool called the HTML Converter to mangle the HTML that loaded a web page with the APPLET tag. Thankfully, that tool is no longer needed and you can use the APPLET tag directly, without the mangling.
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.