The mock exam is modeled on the Sun Certification for Java Programmer SCJP 6 exam. The exam has 29 questions. The real exam may be a little tougher than this. You need to score 58.33% correct answers to clear the real exam. Please let us know at ngabrani AT hotmail dot com if you find any issues with this exam. Some differences between this and the real exam are given below.
- The real exam will be tougher than this.
- The real exam will have 60 questions. This one has only 29
- The real exam would have more programmatic questions. Questions of the type - "what will happen when this code is compiled".
- Which of the following are Java keywords? Select the three correct answers.
- external
- implement
- throw
- void
- integer
- private
- synchronize
- 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.
- public void main(String args)
- public static int main(String args[])
- public static void main(String args[])
- static public void MAIN(String args[])
- public static void main(string args[])
- public static void main(String *args)
- Which of these are legal array declarations or definitions? Select the two correct answers.
- int[] []x[];
- int *x;
- int x[5];
- 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.
- Table
- List
- Set
- SortedSet
- Map
- What gets printed when the following program is compiled and run? Select the one correct answer.
- The program does not compile as i is not initialized.
- The program compiles but does not run.
- The program compiles and runs but does not print anything.
- The program prints 0.
- 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); } }
- The program does not compile as i is not initialized.
- The program compiles but does not run.
- The program compiles and runs but does not print anything.
- The program prints 0.
- The program prints 1.
- What gets printed when the following program is compiled and run? Select the one correct answer.
- 0
- 1
- 2
- 3
- 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.
- 3
- 4
- 5
- 6
- 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 -
- 0
- -127
- -128
- -16384
- -32768
- 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.
int i = 5; boolean t = true; float f = 2.3F; double d = 2.3;
- t = (boolean) i;
- f = d;
- d = i;
- i = 5;
- 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); } }
- 4 4 8 6
- 4 4 8 8
- 4 4 6 6
- 4 3 8 6
- 4 3 8 8
- 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 + b
- a - b
- b - a - 1
- b - a + 1
- b - a
- 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'
- 'b'
- 'c'
- '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); } }
- 0
- 1
- 2
- 3
- 10
- 20
- What is the number displayed when the following program is compiled and run.
- Which of the following statements are true. Select the one correct answer.
- Arrays in Java are essentially objects.
- It is not possible to assign one array to another. Individual elements of array can however be assigned.
- Array elements are indexed from 1 to size of array.
- 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.
- array[array.length()]
- array[array.length() - 1]
- array[array.length]
- array[array.length - 1]
- What is the result of compiling and running the following program. Select the one correct answer.
- 1,2
- 5,2
- 1,6
- 5,6
- Which of the following statements are correct. Select the one correct answer.
- Each Java file must have exactly one package statement to specify where the class is stored.
- If a Java file has both import and package statement, the import statement must come before package statement.
- A Java file has at least one class defined.
- 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.
- The program does not compile.
- The program compiles but generates ArrayIndexOutOfBoundsException exception.
- The program prints java
- The program prints check
- The program prints it
- The program prints out
- What all gets printed when the following code is compiled and run. Select the three correct answers.
- 1
- 2
- 3
- 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.
- char
- boolean
- Boolean
- String
- int
- 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.
- By using the keyword package before the variable.
- By using the keyword private before the variable.
- By using the keyword protected before the variable.
- By using the keyword public before the variable.
- 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.
- Employee should be the base class of Manager class.
- Manager should be the base class of Employee class.
- Manager class should include the Employee class as a data member.
- Employee class should include Manager class as a data member.
- 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.
- The method converts an integer to a String.
- The method is used to convert String to an integer, assuming that the String represents an integer.
- The method is used to convert String to Integer class, assuming that the String represents an integer.
- 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.
- The run() method should be directly invoked on the Object.
- The start() method should be directly invoked on the Object.
- The init() method should be directly invoked on the Object.
- 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.
- MIN_PRIORITY (which is defined as 1 in the Thread class.)
- NORM_PRIORITY (which is defined as 5 in the Thread class.)
- MAX_PRIORITY (which is defined as 10 in the Thread class.)
- A thread inherits the priority of its parent thread.
class test {
public static void main(String args[]) {
int i;
do {
i++;
}
while(i < 0);
System.out.println(i);
}
}
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);
}
}
}
}
}
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);
}
}
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);
}
}
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;
}
}
class check {
public static void main(String args[]) {
System.out.println(args[args.length-2]);
}
}
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");
}
}
Answers to Sample Test 2
- c, d, f
- 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.
- a, d
- List
- e. The collection interface Map has two implementation HashMap and Hashtable.
- a. Local variables are not initialized by default. They must be initialized before they are used.
- d. The variable i gets initialized to zero. The while loop does not get executed.
- c. 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.
- 0x17 or 0X17.
- e. The statement "i=2" evaluates to 2. The expression within the if block must evaluate to a boolean.
- e. The range of short primitive type is -32768 to 32767.
- 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.
- a
- e
- b. 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.
- d. 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.
- 5050. The recursive function xyz essentially sums up numbers 1 to num. This evaluates to (num * (num + 1))/2.
- a. 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.
- d. array.length gives the number of elements in the array. As indexes in Java start from 0, d is the correct answer.
- c. In the invocation of call_array, the first element is invoked using call-by-value, and the second using call-by-reference.
- d. 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.
- e. The args array consists of two elements "it" and "out". args.length is set to two.
- 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.
- d,e,f
- e. A data member that does not have public/protected/private is accessible to all methods in the same package.
- a. 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.
- b. 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.
- b. The start() method invokes the run() method when the thread is ready to execute.
- d
No comments:
Post a Comment