jscp java certificate exam mock questions and answers

jscp java certificate exam mock questions and answers

Postby haretu » Tue Jul 05, 2011 2:12 pm

Question 1

class MCZ11 {
public static void main (String[] args) {
char a = '\c'; // 1
char b = '\r'; // 2
char c = '\"'; // 3
char d = '\b'; // 4
char e = '\''; // 5
}}
A compile-time error is generated at which line?

a. 1
b. 2
c. 3
d. 4
e. 5
f. None of the above
Question 2

class GFM11{
public static void main (String[] args) {
int x,y,z;
System.out.println(x+y+z);
}}
What is the result of attempting to compile and run the program?

a. Prints nothing.
b. Prints an undefined value.
c. Prints: null
d. Prints: 0
e. Run-time error
f. Compile-time error
g. None of the above
Question 3

class MCZ27 {
public static void main (String[] args) {
char a = '\f'; // 1
char b = '\n'; // 2
char c = '\r'; // 3
char d = '\l'; // 4
char e = '\\'; // 5
}}
A compile-time error is generated at which line?

a. 1
b. 2
c. 3
d. 4
e. 5
f. None of the above
Question 4

class GRC4 {public static void main(String[] args) {}} // 1
class GRC5 {public static void main(String []args) {}} // 2
class GRC6 {public static void main(String args[]) {}} // 3
What is the result of attempting to compile and run the above programs?

a. Compile-time error at line 1.
b. Compile-time error at line 2.
c. Compile-time error at line 3.
d. An attempt to run GRC4 from the command line fails.
e. An attempt to run GRC5 from the command line fails.
f. An attempt to run GRC6 from the command line fails.
g. None of the above
Question 5

class MCZ28 {
public static void main (String[] args) {
char a = '\b'; // 1
char b = '\d'; // 2
char c = '\f'; // 3
char d = '\t'; // 4
char e = '\"'; // 5
}}
A compile-time error is generated at which line?

a. 1
b. 2
c. 3
d. 4
e. 5
f. None of the above
Question 6

class JJF1 {
public static void main (String args[]) {
System.out.print(Byte.MIN_VALUE+",");
System.out.print(Byte.MAX_VALUE);
}}
What is the result of attempting to compile and run the program?

a. Prints: 0,255
b. Prints: 0,256
c. Prints: -127,128
d. Prints: -128,127
e. Compile-time error
f. Run-time error
g. None of the above
Question 7

class GFC100 {
public static void main(String[] args) {
final short s1 = 1; // 1
final char c1 = 1; // 2
byte b1 = s1; // 3
byte b2 = c1; // 4
byte b3 = 1; // 5
byte b4 = 1L; // 6
byte b5 = 1.0; // 7
byte b6 = 1.0d; // 8
}}
Compile-time errors are generated at which lines?

a. 1
b. 2
c. 3
d. 4
e. 5
f. 6
g. 7
h. 8
Question 8

class MWC101 {
public static void main(String[] args) {
int[] a1 = new int[]; // 1
int a2[] = new int[5]; // 2
int[] a3 = new int[]{1,2}; // 3
int []a4 = {1,2}; // 4
int[] a5 = new int[5]{1,2,3,4,5}; // 5
}}
Compile-time errors are generated at which lines?

a. 1
b. 2
c. 3
d. 4
e. 5
Question 9

class GRC1 {public static void main(String[] args) {}} // 1
class GRC2 {protected static void main(String[] args) {}} // 2
class GRC3 {private static void main(String[] args) {}} // 3
What is the result of attempting to compile each of the three class declarations and invoke each main method from the command line?

a. Compile-time error at line 1.
b. Compile-time error at line 2.
c. Compile-time error at line 3.
d. An attempt to run GRC1 from the command line fails.
e. An attempt to run GRC2 from the command line fails.
f. An attempt to run GRC3 from the command line fails.
Question 10

class JJF2 {
public static void main (String args[]) {
System.out.print(Short.MIN_VALUE+",");
System.out.print(Short.MAX_VALUE);
}}
What is the result of attempting to compile and run the program?

a. Prints: -32767,32768
b. Prints: -32768,32767
c. Prints: 0,65535
d. Prints: 0,65536
e. Compile-time error
f. Run-time error
g. None of the above
Question 11

class GFC101 {
public static void main(String[] args) {
byte b1 = -128; // 1
byte b2 = 128; // 2
short s1 = -32769; // 3
short s2 = 32767; // 4
char c1 = 0; // 5
char c2 = 65536; // 6
}}
Compile-time errors are generated at which lines?

a. 1
b. 2
c. 3
d. 4
e. 5
f. 6
Question 12

class MWC102 {
public static void main (String[] args) {
byte[] a = new byte[1]; long[] b = new long[1];
float[] c = new float[1]; Object[] d = new Object[1];
System.out.print(a[0]+","+b[0]+","+c[0]+","+d[0]);
}}
What is the result of attempting to compile and run the program?

a. Prints: 0,0,0,null
b. Prints: 0,0,0.0,null
c. Compile-time error
d. Run-time error
e. None of the above
Question 13

class JJF3 {
public static void main(String args[]) {
System.out.print(Integer.toBinaryString(Byte.MAX_VALUE)+",");
System.out.print(Integer.toOctalString(Byte.MAX_VALUE)+",");
System.out.print(Integer.toString(Byte.MAX_VALUE)+",");
System.out.print(Integer.toHexString(Byte.MAX_VALUE));
}}
What is the result of attempting to compile and run the program?

a. Prints: 1111111,177,127,7f
b. Prints: 11111111,377,256,ff
c. Compile-time error
d. Run-time error
e. None of the above
Question 14

class GFC102 {
public static void main(String[] args) {
byte b1 = -129; // 1
byte b2 = 127; // 2
short s1 = -32768; // 3
short s2 = 32768; // 4
char c1 = -1; // 5
char c2 = 65535; // 6
}}
Compile-time errors are generated at which lines?

a. 1
b. 2
c. 3
d. 4
e. 5
f. 6
Question 15

Which of these words belong to the set of Java keywords?

a. transient
b. serializable
c. runnable
d. run
e. volatile
f. externalizable
g. cloneable
Question 16

class GFM12 {
static int x; // 1
public static void main(String[] args) { // 2
int y; // 3
System.out.println("x="+x); // 4
System.out.println("y="+y); // 5
}}
What is the result of attempting to compile and run the program?

a. Compile-time error at line 1.
b. Compile-time error at line 2.
c. Compile-time error at line 3.
d. Compile-time error at line 4.
e. Compile-time error at line 5.
f. Run-time error
g. None of the above
Question 17

class GFM13 {
static byte a; static short b; static char c;
static int d; static long e; static String s;
public static void main(String[] args) {
System.out.println(a+b+c+d+e+s);
}}
What is the result of attempting to compile and run the program?

a. Prints: 00000null
b. Prints: 00000
c. Prints: 0null
d. Prints: 0
e. Prints: null
f. Compile-time error
g. Run-time error
h. None of the above
Question 18

Which of these words belong to the set of Java keywords?

a. virtual
b. goto
c. ifdef
d. typedef
e. friend
f. struct
g. implements
h. union
i. const
Question 19

class Identifiers {
int i1; // 1
int _i2; // 2
int i_3; // 3
int #i4; // 4
int $i5; // 5
int %i6; // 6
int i$7; // 7
int 8i; // 8
}
Compile-time errors are generated at which lines?

a. 1
b. 2
c. 3
d. 4
e. 5
f. 6
g. 7
h. 8


Answers: Java Programmer Certification Mock Exam
No. Answer Remark
1 a 1 The escape sequences are as follows: '\b' (backspace), '\f' (formfeed), '\n' (newline), '\r' (carriage return), '\t' (horizontal tab), '\\' (backslash), '\"' (double quote), '\'' (single quote). Yes, you must memorize the escape sequences! Just remember "big farms need red tractors".
2 f Compile-time error Variables declared inside of a block or method are called local variables; they are not automatically initialized. The compiler will generate an error as a result of the attempt to access the local variables before a value has been assigned.
3 d 4 The escape sequences are as follows: '\b' (backspace), '\f' (formfeed), '\n' (newline), '\r' (carriage return), '\t' (horizontal tab), '\\' (backslash), '\"' (double quote), '\'' (single quote). Yes, you must memorize the escape sequences! Just remember "big farms need red tractors".
4 g None of the above Section 12.1.4 of the Java Language Specification requires that the main method must accept a single argument that is an array of components of type String. In each of the three class declarations, the single argument is indeed an array of components of type String. Please note that the square brackets within an array declaration may appear as part of the type or part of the declarator (i.e. array name).
5 b 2 The escape sequences are as follows: '\b' (backspace), '\f' (formfeed), '\n' (newline), '\r' (carriage return), '\t' (horizontal tab), '\\' (backslash), '\"' (double quote), '\'' (single quote). Yes, you must memorize the escape sequences! Just remember "big farms need red tractors".
6 d Prints: -128,127 A byte is an 8 bit signed value; so the minimum byte value is -(27) and the maximum value is (27 - 1).
7 f g h 6 7 8 The compiler will implicitly do a narrowing conversion for an assignment statement if the right hand operand is a compile time constant of type byte, short, char, or int and the value falls within the range of the variable on the left and if the variable is of type byte, short, or char.
8 a e 1 5 An array creation expression must have either a dimension expression or an initializer. If both are present, then a compile-time error is generated. Similarly, if neither is present, then a compile-time error is generated. If only the dimension expression is present, then an array with the specified dimension is created with all elements set to the default values. If only the initializer is present, then an array will be created that has the required dimensions to accommodate the values specified in the initializer. Java avoids the possibility of an incompatible dimension expression and initializer by not allowing both to appear in the same array creation expression. A compile-time error is generated by the array creation expression for a1, because it needs either a dimension expression or an initializer. A compile-time error is generated at 5, because either the dimension expression or the initializer must be removed.
9 e f An attempt to run GRC2 from the command line fails. An attempt to run GRC3 from the command line fails. Section 12.1.4 of the JLS requires the main method to be declared public. The main methods of GRC2 and GRC3 are not declared public and can not be invoked from the command line using a JVM that is compliant with section 12.1.4. Not every JVM enforces the rule. Even so, for the purposes of the SCJP exam, the main method should be declared as required by the JLS.
10 b Prints: -32768,32767 A short is a 16 bit signed value; so the minimum short value is -(215) and the maximum value is (215 - 1).
11 b c f 2 3 6 The maximum value of type byte is 127. The minimum value of type short is -32768. The maximum value of type char is 65535.
12 b Prints: 0,0,0.0,null Each array contains the default value for its type. The default value of a primitive byte or a primitive long is printed as 0. The default value of a float primitive is printed as 0.0. The default value of an Object is null and is printed as null.
13 a Prints: 1111111,177,127,7f A byte is an 8 bit signed value. The left most bit is the sign bit. The sign bit is set to zero for positive numbers and is set to one for negative numbers. The most positive byte value is represented as a sign bit that is set to zero and all of the other bits set to one. The Integer.toBinaryString method does not print leading zeros; so only seven bits are printed. An eight bit binary value is represented as three octal digits. The first of the three digits represents the left most two bits of the binary value. In this case, the left most two bits are zero and one. The second octal digit represents the next three bits of the binary value. The last of the octal digits represents the right most three bits of binary value. Note that the Integer.toOctalString method does not print a leading zero as is required for an octal literal value. An eight bit binary value is represented as two hexadecimal digits. The left hex digit represents the left most four bits of the binary value. The right hex digit represents the right most four bits of the binary value.
14 a d e 1 4 5 The minimum value of type byte is -128. The maximum value of type short is 32767. The minimum value of type char is 0.
15 a e transient volatile Serializable, Runnable, Externalizable, and Cloneable are all interfaces. Thread.run is a method. The keywords transient and volatile are field modifiers.
16 e Compile-time error at line 5. The local variable y has not been initialized so the attempt to access the variable results in a compile-time error.
17 c Prints: 0null The numeric sum of variables a, b, c, d and e is zero. The zero is converted to a String and concatenated with s.
18 b g i goto implements const The words virtual, ifdef, typedef, friend, struct and union are all words related to the C programming language. Although the words const and goto are also related to the C programming language, they are also Java keywords.
19 d f h 4 6 8 The first letter of an identifier can be any Unicode JLS 3.1 character that is a Java letter. The first letter can not be a number. For historical reasons, the dollar sign $ and underscore _ are considered Java letters along with many currency symbols in use in the world today.
haretu
 
Posts: 132
Joined: Sat Jan 08, 2011 9:56 pm

Re: jscp java certificate exam mock questions and answers

Postby haretu » Tue Jul 05, 2011 2:43 pm

Question 1

class GRC7 {public void main(String[] args) {}} // 1
class GRC8 {public void main(String []args) {}} // 2
class GRC9 {public void main(String args[]) {}} // 3
What is the result of attempting to compile and run the above programs?

a. Compile-time error at line 1.
b. Compile-time error at line 2.
c. Compile-time error at line 3.
d. An attempt to run GRC7 from the command line fails.
e. An attempt to run GRC8 from the command line fails.
f. An attempt to run GRC9 from the command line fails.
Question 2

class MCZ29 {
public static void main (String[] args) {
char a = '\a'; // 1
char b = '\b'; // 2
char c = '\f'; // 3
char d = '\n'; // 4
char e = '\r'; // 5
}}
A compile-time error is generated at which line?

a. 1
b. 2
c. 3
d. 4
e. 5
f. None of the above
Question 3

class MCZ30 {
public static void main (String[] args) {
char a = '\t'; // 1
char b = '\\'; // 2
char c = '\?'; // 3
char d = '\"'; // 4
char e = '\''; // 5
}}
A compile-time error is generated at which line?

a. 1
b. 2
c. 3
d. 4
e. 5
f. None of the above
Question 4

Which of these words belongs to the set of Java keywords?

a. qualified
b. record
c. repeat
d. restricted
e. label
f. to
g. type
h. until
i. value
j. virtual
k. xor
l. None of the above
Question 5

class MCZ31 {
public static void main (String[] args) {
char a = '\t'; // 1
char b = '\\'; // 2
char c = '\"'; // 3
char d = '\''; // 4
char e = '\?'; // 5
}}
A compile-time error is generated at which line?

a. 1
b. 2
c. 3
d. 4
e. 5
f. None of the above
Question 6

Which of these words belong to the set of Java keywords?

a. exit
b. strictfp
c. enum
d. super
e. abort
f. event
g. goto
h. native
i. exception
Question 7

class MCZ13 {
public static void main (String[] args) {
String s = null;
System.out.print(s);
}}
What is the result of attempting to compile and run the program?

a. Prints nothing.
b. Prints: null
c. Compile-time error
d. Run-time error
e. None of the above
Question 8

class MWC103 {
public static void main(String[] args) {
int[] a1 = {1,2,3};
int []a2 = {4,5,6};
int a3[] = {7,8,9};
System.out.print(a1[1]+","+a2[1]+","+a3[1]);
}}
What is the result of attempting to compile and run the program?

a. Prints: 12
b. Prints: 15
c. Prints: 18
d. Prints: 1,4,7
e. Prints: 2,5,8
f. Prints: 3,6,9
g. Compile-time error
h. Run-time error
i. None of the above
Question 9

class JJF4 {
public static void main(String args[]) {
System.out.print(Long.toHexString(Byte.MAX_VALUE)+",");
System.out.print(Long.toHexString(Character.MAX_VALUE)+",");
System.out.print(Long.toHexString(Short.MAX_VALUE));
}}
What is the result of attempting to compile and run the program?

a. Prints: f,ff,7f
b. Prints: f,ff,ff
c. Prints: 7f,ffff,7fff
d. Prints: ff,ffff,ffff
e. Prints: 7fff,ffffff,7fffff
f. Prints: ffff,ffffff,ffffff
g. Compile-time error
h. Run-time error
i. None of the above
Question 10

class MWC104 {
public static void main(String[] args) {
int[5] a1; // 1
int []a2; // 2
int[ ]a3; // 3
int a4[]; // 4
}}
A compile-time error is generated at which line?

a. 1
b. 2
c. 3
d. 4
e. None of the above
Question 11

class JJF5 {
public static void main(String args[]) {
System.out.print(Integer.toHexString(Integer.MIN_VALUE)+",");
System.out.print(Integer.toHexString(Integer.MAX_VALUE));
}}
What is the result of attempting to compile and run the program?

a. Prints: 0000,ffff
b. Prints: 00000000,ffffffff
c. Prints: 7fff,8000
d. Prints: 8000,7fff
e. Prints: 7fffffff,80000000
f. Prints: 80000000,7fffffff
g. Compile-time error
h. Run-time error
i. None of the above
Question 12

Which of the following represent the full range of type char?

a. '\u0000' to '\u7fff'
b. '\u0000' to '\uffff'
c. 0 to 32767
d. 0 to 65535
e. -32768 to 32767
f. -65536 to 65535
Question 13

Which of the following represent the full range of type char?

a. -0x8000 to 0x7fff
b. 0x0000 to 0xffff
c. -0100000 to 077777
d. 0 to 0177777
e. 0 to 32767
f. 0 to 65535
g. -32768 to 32767
Question 14

class GFM14 {
static byte a; static short b; static char c;
static int d; static long e; static String s;
public static void main(String[] args) {
System.out.println(a+","+b+","+(int)c+","+d+","+e+","+s);
}}
What is the result of attempting to compile and run the program?

a. Prints: 0,0,0,0,0,null
b. Prints: 0,0,0,0,0,
c. Prints: 0,0, ,0,0,
d. Compile-time error
e. Run-time error
f. None of the above
Question 15

class GFM15 {
static int a; static float b; static double c;
static boolean d; static String s;
public static void main(String[] args) {
System.out.println(a+","+b+","+c+","+d+","+s);
}}
What is the result of attempting to compile and run the program?

a. Prints: 0,0,0,false,null
b. Prints: 0,0,0,false,
c. Prints: 0,0.0,0.0,false,null
d. Prints: 0,0.0,0.0,false,
e. Prints: 0,0.0,0.0,true,null
f. Prints: 0,0.0,0.0,true,
g. Prints: 0,0,0,true,null
h. Prints: 0,0,0,true,
i. Compile-time error
j. Run-time error
k. None of the above
Question 16

class MWC105 {
static boolean b1;
public static void main(String[] args) {
boolean[] array = new boolean[1];
boolean b2;
System.out.print(b1+",");
System.out.print(array[0]+",");
System.out.print(b2);
}}
What is the result of attempting to compile and run the program?

a. Prints: true,true,true
b. Prints: false,false,false
c. Prints: null,null,null
d. Prints: false,true,false
e. Compile-time error
f. Run-time error
g. None of the above
Question 17

class GFM16 {
static int m1 (int i1, int i2) {
int i3;
if (i1 > 0) {i3 = i1 + i2;}
return i3;
}
public static void main(String[] args) {
System.out.println(m1(1,2));
}}
What is the result of attempting to compile and run the program?

a. Prints: 0
b. Prints: 1
c. Compile-time error
d. Run-time error
e. None of the above
Question 18

Which of these lists contains at least one word that is not a Java keyword?

a. abstract, default, if, private, this
b. do, implements, protected, boolean, throw
c. case, extends, int, short, try
d. import, break, double, exception, throws
e. byte, else, instanceof, return, transient
f. None of the above
Question 19

Which of these lists contains at least one word that is not a Java keyword?

a. interface, static, void, catch, final
b. char, strictfp, finally, long, volatile
c. native, super, class, float, while
d. const, for, new, switch, import
e. continue, finalize, goto, package, synchronized
f. None of the above

Answers: Java Programmer Certification Mock Exam
No. Answer Remark
1 d e f An attempt to run GRC7 from the command line fails. An attempt to run GRC8 from the command line fails. An attempt to run GRC9 from the command line fails. Section 12.1.4 of the JLS requires the main method to be declared static. In this example, each of the three main methods are not declared static. The result is an error at run-time.
2 a 1 The escape sequences are as follows: '\b' (backspace), '\f' (formfeed), '\n' (newline), '\r' (carriage return), '\t' (horizontal tab), '\\' (backslash), '\"' (double quote), '\'' (single quote). Yes, you must memorize the escape sequences! Just remember "big farms need red tractors".
3 c 3 The escape sequences are as follows: '\b' (backspace), '\f' (formfeed), '\n' (newline), '\r' (carriage return), '\t' (horizontal tab), '\\' (backslash), '\"' (double quote), '\'' (single quote). Yes, you must memorize the escape sequences! Just remember "big farms need red tractors".
4 l None of the above All of these are keywords of the Pascal programming language, but none are Java keywords.
5 e 5 The escape sequences are as follows: '\b' (backspace), '\f' (formfeed), '\n' (newline), '\r' (carriage return), '\t' (horizontal tab), '\\' (backslash), '\"' (double quote), '\'' (single quote). Yes, you must memorize the escape sequences! Just remember "big farms need red tractors".
6 b d g h strictfp super goto native
7 b Prints: null The System.out.print method prints the word null if the argument is a String reference that is null.
8 e Prints: 2,5,8 Arrays a1, a2 and a3 all contain 3 integers. The first element of the array has an index of 0 so an index of one refers to the second element of each array.
9 c Prints: 7f,ffff,7fff A byte is an 8 bit signed value. A char is a 16 bit unsigned value. A short is a 16 bit signed value. The left most bit of a signed value is the sign bit. The sign bit is zero for positive numbers and one for negative numbers. The maximum byte value in hexadecimal format is 7f and in decimal format is 127. The minimum byte value in hexadecimal format is 80 and in decimal format is -128. The byte value of decimal -1 is ff in hexadecimal.
10 a 1 A compile-time error occurs at the line marked 1, because the array reference declaration can not be used to declare the number of components contained in the array. Instead, the dimension expression should be contained in an array creation expression such as new int[5].
11 f Prints: 80000000,7fffffff An int is a 32 bit signed value. The left most bit is the sign bit. The sign bit is zero for positive numbers and one for negative numbers.
12 b d '\u0000' to '\uffff' 0 to 65535 A char is a 16 bit unsigned value; so none of the char values are negative and the minimum value is zero. The maximum value is 216 - 1.
13 b d f 0x0000 to 0xffff 0 to 0177777 0 to 65535 A char is a 16 bit unsigned value; so none of the char values are negative and the minimum value is zero. The maximum value is 216 - 1.
14 a Prints: 0,0,0,0,0,null The default value of type char is the null character. When it is cast to an int the value is interpreted as zero.
15 c Prints: 0,0.0,0.0,false,null Generally speaking, numeric type variables are initialized to zero. Primitive boolean variables are initialized to false. Reference type variables are initialized to null.
16 e Compile-time error Variable b1 is initialized to false, because it is a class member. The array component array[0] is initialized to the default value, false, of the array type, boolean[], even though the array is declared locally. Local variable b2 is not initialized, because it is local. A compile-time error is generated by the statement that attempts to print the value of b2.
17 c Compile-time error Local variables are not initialized automatically, and must be initialized explicitly before attempting to access the value. The local variable i3 will not be initialized if i1 is less than or equal to zero; so the result is a compile-time error.
18 d import, break, double, exception, throws The word exception is not a Java keyword. The words import, break, double and throws are Java keywords.
19 e continue, finalize, goto, package, synchronized The word finalize is the name of a method of the Object class: It is not a keyword. The words continue, goto, package and synchronized are all Java keywords.
haretu
 
Posts: 132
Joined: Sat Jan 08, 2011 9:56 pm

Re: jscp java certificate exam mock questions and answers

Postby haretu » Tue Jul 05, 2011 3:16 pm

Question 1

Which of these words belong to the set of Java keywords?

a. Double
b. goto
c. min
d. extern
e. new
f. signed
g. finally
h. const
i. and
j. array
k. do
Question 2

class MCZ15 {
public static void main (String[] args) {
float a = 1.1e1f; // 1
float b = 1e-1F; // 2
float c = .1e1f; // 3
double d = .1d; // 4
double e = 1D; // 5
}}
A compile-time error is generated at which line?

a. 1
b. 2
c. 3
d. 4
e. 5
f. None of the above
Question 3

Which of these words belong to the set of Java keywords?

a. declare
b. global
c. const
d. preserve
e. continue
f. Float
g. extends
h. select
i. break
j. dim
k. instanceOf
Question 4

class MCZ16 {
public static void main (String[] args) {
float a = 1; // 1
float b = 1L; // 2
float c = 1F; // 3
float d = 1.0; // 4
}}
A compile-time error is generated at which line?

a. 1
b. 2
c. 3
d. 4
e. None of the above
Question 5

Which of these words belong to the set of Java keywords?

a. next
b. catch
c. function
d. instanceof
e. mod
f. const
g. or
h. Boolean
i. goto
j. import
k. transient
Question 6

class MCZ17 {
public static void main (String[] args) {
String a = "\n"; // 1
String b = "\r"; // 2
String c = "\u000a"; // 3 \u000a = new line
String d = "\u000d"; // 4 \u000d = return
}}
Compile-time errors are generated at which lines?

a. 1
b. 2
c. 3
d. 4
Question 7

Which of these words belong to the set of Java keywords?

a. byte
b. short
c. int
d. long
e. decimal
f. int64
g. float
h. single
i. double
j. boolean
k. char
l. unsigned
m. array
n. string
Question 8

class MCZ18 {
public static void main (String[] args) {
String a = "abcd"; // 1
String b = "'\u0041'"; // 2
String c = "\u0041"; // 3
String d = "\uD7AF"; // 4
System.out.print(a+b+c+d); // 5
}}
A compile-time error is generated at which line?

a. 1
b. 2
c. 3
d. 4
e. 5
f. None of the above
Question 9

Which of these words belongs to the set of Java keywords?

a. clause
b. loop
c. expression
d. overrides
e. statement
f. assertion
g. validate
h. exception
i. cast
j. None of the above
Question 10

class MCZ20 {
public static void main (String[] args) {
// Insert code here.
}}
Which of the following lines can be inserted at the specified location without generating a compile-time error?

a. String a = 'a';
b. String b = 'abc';
c. String c = '\u0041';
d. String d = '\uabcd';
e. None of the above
Question 11

class MCZ21 {
public static void main (String[] args) {
// Insert code here.
}}
Which of the following lines can be inserted at the specified location without generating a compile-time error?

a. char a = a;
b. char b = abc;
c. char c = \u0041;
d. char d = \uabcd;
e. None of the above
Question 12

class MCZ24 {
public static void main (String[] args) {
char a = 061; // 1
char b = '\61'; // 2
char c = '\061'; // 3
char d = 0x0031; // 4
char e = '\u0031'; // 5
System.out.print(""+a+b+c+d+e);
}}
A compile-time error is generated at which line?

a. 1
b. 2
c. 3
d. 4
e. 5
f. None of the above
Question 13

class MCZ25 {
public static void main (String[] args) {
char a = '\7'; // 1
char b = '\61'; // 2
char c = '\062'; // 3
char d = '\x7'; // 4
char e = '\x41'; // 5
System.out.print(""+a+b+c+d+e);
}}
Compile-time errors are generated at which lines?

a. 1
b. 2
c. 3
d. 4
e. 5
Question 14

class JJF6 {
char c1 = 0xffff; // 1
char c2 = 0xfffff; // 2
byte b1 = 0xffff; // 3
byte b2 = 0x7f; // 4
byte b3 = 0xff; // 5
byte b4 = -0x80; // 6
}
Compile-time errors are generated at which lines?

a. 1
b. 2
c. 3
d. 4
e. 5
f. 6
Question 15

class MCZ19 {
public static void main (String[] args) {
String a1 = null; // 1
String b1 = 'null'; // 2
String c1 = "null"; // 3
String d1 = "'null'"; // 4
}}
A compile-time error is generated at which line?

a. 1
b. 2
c. 3
d. 4
e. None of the above
Question 16

class MCZ22 {
public static void main (String[] args) {
// Insert code here.
}}
Which of the following lines will generate a compile-time error if inserted at the specified location?

a. char a = 0x0041;
b. char b = '\u0041';
c. char c = 0101;
d. char d = -1;
e. char e = (char)-1;
f. None of the above
Question 17

class MCZ23 {
public static void main (String[] args) {
// Insert code here.
}}
Which of the following lines can be inserted at the specified location without generating a compile-time error?

a. boolean b1 = true;
b. boolean b2 = TRUE;
c. boolean b3 = 'true';
d. boolean b4 = "TRUE";
e. boolean b5 = 0;
f. None of the above
Question 18

class GFM17 {
int x; // 1
public static void main(String[] args) { // 2
int y = 0; // 3
System.out.print(x+","); // 4
System.out.print(y); // 5
}}
What is the result of attempting to compile and run the program?

a. Prints: 0,0
b. Compile-time error at line 1.
c. Compile-time error at line 1.
d. Compile-time error at line 2.
e. Compile-time error at line 3.
f. Compile-time error at line 4.
g. Compile-time error at line 5.
h. Run-time error
i. None of the above

Answers: Java Programmer Certification Mock Exam
No. Answer Remark
1 b e g h k goto new finally const do
2 f None of the above Floating-point literals are covered in section 3.10.2 of the JLS. A floating-point literal can begin with either a digit or a decimal point. Optionally, it can have a fractional part, an exponent part and a floating point suffix--f, F, d, or D.
3 c e g i const continue extends break All of the letters of all Java keywords are lower case. The word instanceof is a Java keyword, but instanceOf is not.
4 d 4 The literal 1.0 is a double and can not be used to initialize a float without an explicit cast.
5 b d f i j k catch instanceof const goto import transient
6 c d 3 4 The compiler interprets \u000a as a line terminator. The escape sequence \n should be used instead. Similarly, \u000d is interpreted as a line terminator. The escape sequence \r should be used instead.
7 a b c d g i j k byte short int long float double boolean char
8 f None of the above All of the declarations are legal. String b is a single quote followed by the letter A followed by another single quote. String c is the letter A. String d is the Unicode character that is represented by the hexadecimal value D7AF. String literals are covered in section 3.10.5 of the JLS.
9 j None of the above
10 e None of the above String literals are declared using double quotes, but all of the declarations here use single quotes.
11 e None of the above Unicode char literals are declared using single quotes, but none of the declarations here use single quotes. The declaration of char b, is also problematic, because it contains more than one char.
12 f None of the above All of the declarations are legal. The first three ( 061, '\61', '\061' ) are declared in octal format. The fourth (0x0031) is declared as a hexadecimal literal. The fifth ('\u0031') is a Unicode escape sequence.
13 d e 4 5 All of the escape sequences used in this question are defined for the C programming language. Those that are not also Java escape sequences result in a compile-time error. Java does not accept the hexadecimal escape sequences of the C programming language. However, Java does accept Unicode escapes (JLS 3.3).
14 b c e 2 3 5 The maximum char value is 0xffff. The maximum byte value is 127 = 0x7f. The hex value 0xff is of type int, and the decimal value is 255. The maximum positive value of type byte is 127. The value 255 is beyond the range of type byte; so 255 can not be assigned to type byte without an explicit cast. The assignment expression b3 = (byte)0xff would assign the value -1 to variable b3.
15 b 2 The reference a1 is set to null. String b1 generates a compile-time error, because String literals must be enclosed by double quotes. String c1 is the word null. String d1 is a single quote followed by the word null followed by another single quote. String literals are covered in section 3.10.5 of the JLS.
16 d char d = -1; The assignment of -1 to char d generates a compile-time error, because the primitive char type is unsigned. A negative int can not be assigned to a char without an explicit cast. If the literal value -1 were cast to type char then the result would be \uffff.
17 a boolean b1 = true; There are two primitive boolean values: true and false. Both must be written with lower case letters. Although the C programming language accepts zero as a boolean value, the Java programming language does not.
18 f Compile-time error at line 4. Anytime a field is accessed from within a static context it is very important to verify that the field is also static. If the field is instead an instance variable then the result is a Compile-time error.
haretu
 
Posts: 132
Joined: Sat Jan 08, 2011 9:56 pm

Re: jscp java certificate exam mock questions and answers

Postby haretu » Sun Jul 10, 2011 8:40 pm

** ================
1.1 Which of the following are valid constructors of the class java.lang.AssertionError?
A. AssertionError()
B. AssertionError(String detailMessage)
C. AssertionError(boolean detailMessage)
D. AssertionError(int detailMessage)
E. AssertionError(StackTraceElement detailMessage)
Answer: ACD

**** ================
1.2 What should be inserted at line 7 so that the code compiles and guarantees to print -
Apple Cricket Banana in the same order.
1. import java.util.*;
2.
3. public class SetTest
4. {
5. public static void main(String args[])
6. {
7. // what should be inserted here?
8.
9. set.add("Apple");
10. set.add("Cricket");
11. set.add("Banana");
12.
13. Iterator iter = set.iterator();
14. while(iter.hasNext())
15. {
16. System.out.print(iter.next() + " ");
17. }
18. }
19. }
A. Set set = new LinkedHashSet();
B. Set set = new HashSet();
C. Set set = new TreeSet();
D. TreeSet maintains the Set elements sorted according to their natural order; however other sets do not guarantee iteration order of the set. In particular, they do not guarantee that the order will remain constant over time.
Answer: A
Note: JDK 1.4 adds a new collection class LinkedHashSet to the collections framework. LinkedHashSet remembers the order in which objects are inserted.
TreeSet maintains its elements in their natural order, hence iterating will produce "Apple Banana Cricket " instead of "Apple Cricket Banana ". Hence option C is incorrect.
HashSet is a basic implementation of Set. It makes no guarantees as to the iteration order of the set;in particular.

**** ==============================
1.3 What will happen when you attempt to compile and run the following code?
public class Test{
int i = 0;
public static void main(String argv[]) {
Test t = new Test();
t.myMethod();
}
public void myMethod(){
while(true) {
try {
wait();
}catch (InterruptedException e) {}
i++;
}
}
}
A. Compile time error, no matching notify within the method.
B. Compile and run but an infinite looping of the while method.
C. Compilation and run without any output.
E. Runtime Exception "IllegalMonitorStatException".
Answer: E
Note: The wait/notify protocol can only be used within code that is synchronized. In this case calling code does not have a lock on the object(not synchronized) and will thus cause an Exception at runtime.

***** ====================
1.4 What will be the result of executing the following main() method?
1. public static void main(String[] args)
2. {
3. String myString;
4. int x = 100;
5.
6. if (x < 100) myString = "x is less than 100";
7. if (x > 100) myString = "x is greater than 100";
8. System.out.println(myString.length());
9. }
A. The code will compile with a warning that variable myString might not have been initialized
B. The code will compile without any warnings
C. The code will not compile. But it will compile and print the length of myString if the variable myString is initialized in an unconditional statement before line 8
D. None of these
Answer: D??
Note: myString may be initiated to be null and then run time error.

**** ==================
1.5 What results from running the code below?
int a = -5; int b = -2;
System.out.println(a % b);
A. 0
B. 1
C. -1
D. A compiler error saying "%" operator is not valid for negative numbers.
Answer: C
Note: To calculate a % b(a modulus b), the simplest way is, drop any negative signs from both the operands and calculate the result. Now if the left-hand side operand is negative negate the result, the sign of right-hand side operand doesn't matter. In our case (-5 % -2) is equal to -(5 % 2) = -(1) = -1, hence C is correct. If the calculate a%b(a=5, b=-2), the result will be 1.

** =================
1.6 What is the return type of method add(Object o) of Set interface, and what does it indicate?
A. It returns an int representing hash code of the newly added object
B. It returns an Integer object representing hash code of the newly added object
C. It returns a boolean. Returns true if this set already contains the specified element.
D. It returns a boolean. Returns true if this set did not already contain the specified element.
E. Return type is void. Any error in the add operation is reported by throwing an exception.
F. None of these
Answer: D

*** =====================
1.7 Which of the following classes implement java.util.Map interface?
A. Hashtable
B. HashMap
C. Dictionary
D. IdentityHashMap
E. Vector
Answer: ABD
Note: The abstract class Dictionary is a legacy class and as per Java API documentation, it is obsolete. It does not implement Map interface.

** ===================
1.8 The package favorite.fruits contains different classes for different fruits. Orange is one of the classes in this package. Assume that all the classes from this package are compiled with assertions enabled. Which of the following will enable assertions at runtime for this package, but will disable it for the class Orange, using standard JDK 1.4?
A. java -ea:favorite.fruits... -da:favorite.fruits.Orange <Main Class>
B. java -ea:favorite.fruits...,-da:favorite.fruits.Orange <Main Class>
C. java -ea:favorite.fruits... -da:Orange <Main Class>
D. java -ea:favorite.fruits... -da Orange <Main Class>
E. None of these
Answer: A+++++++++++++++++++++++

**** ========================
1.9 What is the valid declaration for the finalize() method?
A. protected void finalize() throws Throwable
B. final finalize()
C. public final finalize()
D. private boolean finalize()
E. private final void finalize() throws Exception
Answer: A

** ====================
1.10 What is the result of compiling and executing the following code?
public class ThreadTest extends Thread {
public void run() {
System.out.println("In run");
yield();
System.out.println("Leaving run");
}
public static void main(String args []) {
(new ThreadTest()).start();
}
}
A. The code fails to compile in the main() method.
B. The code fails to compile in the run() method.
C. Only the text "In run" will be displayed.
D. The text "In run" followed by "Leaving run" will be displayed.
E. The code compiles correctly, but nothing is displayed.
Answer: D
haretu
 
Posts: 132
Joined: Sat Jan 08, 2011 9:56 pm

Re: jscp java certificate exam mock questions and answers

Postby haretu » Wed Jul 13, 2011 9:19 am

2.1 What will happen when you attempt to compile and run the following code?
(Assume that the code is compiled and run with assertions enabled.)
public class AssertTest {
private void methodA(int i) {
assert i >= 0 : methodB();
System.out.println(i);
}
private String methodB(){
return "The value must not be negative";
}
public static void main(String args[]){
AssertTest test = new AssertTest();
test.methodA(-10);
}
}
A. It will print -10.
B. It will result in AssertionError with the message -"The value must not be negative".
C. The code will not compile.
D. None of these
Answer: B

*** ================
2.2 Which of the following are true?
A. Java uses a time-slicing scheduling system for determining which Thread will execute.
B. Java uses a pre-emptive, co-operative system for determining which Thread will execute.
C. Java scheduling is platform dependent and may vary from one implementation to another.
D. You can set the priority of a Thread in code.
Answer: CD

**** ==================
2.3 Which Java collection class can be used to maintain the entries in the order in which they were last accessed?
A. java.util.HashSet
B. java.util.LinkedHashMap
C. java.util.Hashtable
D. java.util.Vector
E. None of these
Answer: B
Note: LinkedHashMap is a very important class of Java's collection framework; it is important to understand its features and usages.

***** ===================
2.4 What will happen when you attempt to compile and run the following code?
class MyClass {
static String myName = "SCJP";
MyClass getMyClass() {
System.out.println(myName);
return null;
}
public static void main(String[ ] args) {
System.out.println( new MyClass().getMyClass().myName );
}
}
A. Compile time error
B. Run time error
C. Prints SCJP twice
D. Prints SCJP once
E. None of the above
Answer: C
Note: compiler sees the return type of getMyClass method as MyClass and calling myName on MyClass is valid at compile time.
Also it doesn't give any runtime error which is not very obvious. Please note that a null reference may be used to access a class (static) variable without causing an exception. The static variable is called on the class itself and not on the object. Had the myName variable non-static, the above code at run time would give NullPointerException.

*** =====================
2.5 What results from running the code below?
int a = 5; int b = -2;
System.out.println(a % b);
A. A compiler error saying "%" operator is not valid for negative numbers
B. 1
C. 0
D. -1
E. None of the above
Answer: B
Note: To calculate a % b(a modulus b), the simplest way is, drop any negative signs from both the operands and calculate the result.

*** ================
2.6 A block of text contains 100 words. You are required to make a list of distinct words from those 100 words;moreover you are required to report how many distinct words exist in that block of text. Which Java collection class would you use;and which of its method would give the number of distinct words in the block?
A. java.util.LinkedList class, and its size() method
B. java.util.HashSet class, and its size() method
C. java.util.HashMap class, and its size() method
D. java.util.ArrayList class, and its size() method
E. java.util.Dictionary class, and its size() method
Answer: B

*** ================
2.7 What will be printed by the following code?
String banner = "One man, One vote";
int subInd1 = banner.lastIndexOf("One", 10);
System.out.println(subInd1);
A. 0
B. -1
C. 10
D. None of these
Answer: D 9


2.8 What results by running the following code fragment?
int a = 8;
int b = 3;
float f = a++/b--;
System.out.println(f);
A. 3.0
B. 4.5
C. 2.0
D. 2.6
Answer: C
Note: a++/b-- is int, and then implicitly cast to float.

*** ================
2.9 We have the following organization of classes.
class Parent { }
class DerivedOne extends Parent { }
class DerivedTwo extends Parent { }
Which of the following statements is correct for the following expression?
Parent p = new Parent();
DerivedOne d1 = new DerivedOne();
DerivedTwo d2 = new DerivedTwo();
d1 = (DerivedOne)p;
A. Illegal both compile and runtime
B. Legal at compile time, but fails at runtime
C. Legal at compile and runtime
D. None of the above
Answer: B
Note: Since the object contained in p is not actually a DeriveOne object, the assignment will cause Java to throw a ClassCastException.

**** ===================
2.10 Which of the following will compile and run without an error?
A. Integer i = new Integer('A');
B. Integer i = new Integer("7");
C. Character c = new Character("A");
D. Boolean b = new Boolean(null);
E. Integer i = new Integer("0x10");
Answer: ABD
Note: Option E will compile correctly but will throw a NumberFormatException at runtime, as the string argument does not represent a string in decimal format. E???
haretu
 
Posts: 132
Joined: Sat Jan 08, 2011 9:56 pm

Re: jscp java certificate exam mock questions and answers

Postby haretu » Wed Jul 13, 2011 9:20 am

**** =========================
3.1 What will be printed when you execute the following code?
class C {
C() {
System.out.print("C");
}
}
class A {
C c = new C();
A() {
this("A");
System.out.print("A");
}
A(String s) {
System.out.print(s);
}
}
class B extends A {
B() {
super("B");
System.out.print("B");
}
public static void main(String[] args) {
new B();
}
}
A. BB
B. CBB
C. BAB
D. None of these
Answer: B


*** ========================
3.2 Which of the following method(s) must be implemented correctly if the class has to behave properly and consistently with all Java collection classes and interfaces?
A. public int hashCode()
B. protected Object clone()
C. public String toString()
D. public boolean equals(Object obj)
E. public long hashCode()
F. Only option E is correct
Answer: AD


*** ===========================
3.3 If an instance of a class needs to be added to a TreeSet, which interface this class must implement?
(Assume that the TreeSet is instantiated with the default no-argument constructor)
A. java.util.SortedSet
B. java.lang.Cloneable
C. java.lang.Comparable
D. java.util.RandomAccess
E. java.io.Serializable
Answer: C

*** ============================
3.4 The ThreadGroup class instance
A. Allows threads to be manipulated as group.
B. Provides support for ThreadDeath listeners.
C. May contain other ThreadGroups.
D. Must contain threads of the same type.
Answer: AC

***** ==========================
3.4 What will happen when you compile and run the following code?
public class Test {
public void myMethod(Object o) {
System.out.println("My Object");
}
public void myMethod(String s) {
System.out.println("My String");
}
public static void main(String args[]) {
Test t = new Test();
t.myMethod(null);
}
}
A. The code does not compile.
B. The code compiles cleanly and shows "My Object".
C. The code compiles cleanly and shows "My String"//更精确地匹配
D. The code throws an Exception at Runtime.
Answer: C


***** ==========================
3.5 Which of the following results in a negative value of a?
A. int a = -1; a = a >>> 8;
B. byte a = -1; a = (byte)(a >>> 5);//char only has 16 bytes, that is 65535
C. int a = -1; a = a >> 5;
D. int a = -1; a = a >>> 32;
Answer: BCD


**** ==========================
3.6 Which one statement is true about class Animal above and class Tiger below?
1. package abc;
2.
3. public class Animal
4. {
5. protected static int refCount = 0;
6. public Animal() {refCount++;}
7. protected void runFast() { } // Run as fast as you can
8. static int getRefCount() { return refCount; }
9. }

1. package def;
2.
3. class Tiger extends abc.Animal
4. {
5. Tiger() { refCount++; }
6.
7. public static void main(String args[])
8. {
9. System.out.println("Before: " + refCount);
10. Tiger ashtonish = new Tiger();
11. System.out.println("After: " + refCount);
12. ashtonish.runFast();
13. }
14. }
A. Compilation of Tiger fails at line 5 because static members cannot be overridden.
B. Compilation of Tiger fails at line 12 because method runFast() is protected in the superclass.
C. The program will compile and execute. The output will be
Before: 0
After: 1
D. Compilation will succeed, but an exception will be thrown at line 12, because method runFast() is protected in the superclass.
E. The program will compile and execute. The output will be
Before: 0
After: 2
Answer: E

***** ============================
3.7 What is displayed when the following piece of code is compiled and executed?
class Test {
public static void main(String [] args) {
Base b = new Subclass();
System.out.println(b.x);
System.out.println(b.method());
}
}
class Base {
int x = 2;
int method(){
return x;
}
}
class Subclass extends Base {
int x = 3;
int method() {
return x;
}
}
A. Nothing. The code fails to compile because the object b is not created in a valid way.
B. 2 3
C. 2 2
D. 3 3
E. 3 2子类引用赋给父类,运行时按父类变量赋值,而方法则用子类的
Answer: B

** =======================
3.8 What will happen when you attempt to compile and run the following code?
(Assume that the code is compiled and run with assertions enabled.)
1. public class AssertTest
2. {
3. public static void main(String args[])
4. {
5. for(int i=0;i <10;i++)
6. {
7. try
8. {
9. assert i%2==0 : i--;
10. System.out.println("Even number : " + i);
11. }
12. catch(AssertionError ae)
13. {
14. System.out.println("Odd number : " + ++i);
15. }
16. }
17. }
18. }
A. It will print odd and even numbers from 0 to 9 correctly (0 even and 1 odd)
B. It will print odd and even numbers from 0 to 9 incorrectly (0 odd and 1 even)
C. Compilation error at line 9
D. Compilation error at line 10
E. It will result in an infinite loop
Answer: A

** ====================
3.9 What will happen when you attempt to compile and run the following code?
(Assume that the code is compiled and run with assertions enabled)
1. public class AssertTest
2. {
3. public static void main(String args[])
4. {
5. float f1 = Float.NaN;
6. float f2 = f1;
7. float f3 = 1.2f;
8.
9. try
10. {
11. assert (f2 == f1) : f2 = 2;
12. f3 = 1.5f;
13. }catch(AssertionError ae)
14. {
15. f3++;
16. }
17.
18. f3 += f2;
19. System.out.println("f3 = " + f3);
20. }
21. }
A. Compilation error at line 5
B. Compilation error at line 7
C. It will print - f3 = 3.5
D. It will print - f3 = 4.2
E. It will print - f3 = NaN
Answer: D

**** =================
3.10 Which of the following will compile without errors?
A. Byte b = new Byte(123);
B. Byte b = new Byte("123");
C. Byte b = new Byte();//Constructor Summary
Byte(byte value)
Constructs a newly allocated Byte object that represents the specified byte value.
Byte(String s)
Constructs a newly allocated Byte object that represents the byte value indicated by the String parameter.
b = 123;
D. Byte b = new Byte((int)123.4);
Answer: B

**** =====================
3.11 What will happen when we try to compile the following code?
1. public void WhichArray( Object x )
2. {
3. if( x instanceof int[] )
4. {
5. int[] n = (int[]) x ;
6. for( int i = 0 ; i < n.length ; i++ )
7. {
8. System.out.println("integers = " + n[i] );
9. }
10. }
11. if( x instanceof String[] )
12. {
13. System.out.println("Array of Strings");
14. }
15. }
A. The compiler objects to line 3 comparing an Object with an array.
B. The compiler objects to line 5 casting an Object to an array of int primitives.
C. The compiler objects to line 11 comparing an Object to an array of Objects.
D. It compiles without error.
Answer: D

**** ==================
3.12 Which of the following statements about threading are true?
A. You can only obtain a mutually exclusive lock on methods in a class that extends Thread or implements runnable.
B. You can obtain a mutually exclusive lock on any object.
C. You can't obtain a mutually exclusive lock on any object.
D. Thread scheduling algorithms are platform dependent.
Answer: BD
haretu
 
Posts: 132
Joined: Sat Jan 08, 2011 9:56 pm

Re: jscp java certificate exam mock questions and answers

Postby haretu » Wed Jul 13, 2011 9:21 am

***** =======================
4.1 What will happen when you invoke the following method?
void infiniteLoop(){
byte b = 1;
while ( ++b > 0 );
System.out.println("Welcome to My World!");
}
A. The loop never ends(infinite loop).
B. Prints "Welcome to My World!".
C. Compilation error : ++ operator should not be used for byte type variables.
D. Prints nothing.
Answer: B
NOte: Here the variable 'b' will go up to 127. After that overflow will occur and 'b' will be set to -ve value, the loop ends and prints "Welcome to My World!".

**** =======================
4.2 Which of the following statements are true about java.util.IdentityHashMap?
A. It implements Map interface
B. It is synchronized
C. In an IdentityHashMap, two keys k1 and k2 are considered equal if and only if (k1==k2).
D. In an IdentityHashMap, two keys k1 and k2 are considered equal if and only if k1.equals(k2) OR if both k1 and k2 are null
E. It violates general contract of the interface Map
F. It keeps its keys in a sorted order
Answer: ACE

**** ===================
4.3 Which of the following results in a positive value of a?
A. int a = -1; a = a >>> 5;
B. int a = -1; a = a >>> 32;
C. byte a = -1; a = (byte)(a >>> 30);
D. int a = -1; a = a >> 5;
Answer: AC

**** =====================
4.4 Select the statements which are valid from the list below.
A. The primitive data type "char" is 16-bits wide.
B. Unicode uses 16-bits to represent a character.
C. Unicode uses 32-bits to represent a character.
D. UTF uses 24-bits to represent a character.
E. UTF uses as many bits as are needed to represent a character.
Answer: ABE

*** =====================
4.5 You are writing a set of classes related to cooking and have created your own exception hierarchy derived from java.lang.Exception as follows:
Exception
+-- BadTasteException
+-- BitterException
+-- SourException
Your base class, "BaseCook" has a method declared as follows:
int rateFlavor(Ingredient[] list) throws BadTasteException
class "TexMexCook", derived from BaseCook has a method which overrides BaseCook.rateFlavor(). Which of the following are legal declarations of the overriding method?
A. int rateFlavor(Ingredient[] list) throws BadTasteException
B. int rateFlavor(Ingredient[] list) throws Exception
C. int rateFlavor(Ingredient[] list) throws BitterException
D. int rateFlavor(Ingredient[] list)
Answer: ACD
Note: the overriding method must throw the same exception or a subclass.the overriding method need not have to throw an exception.

***** =====================
4.6 Which is the most appropriate code snippet that can be inserted at line 18 in the following code?
(Assume that the code is compiled and run with assertions enabled)
1. import java.util.*;
2.
3. public class AssertTest
4. {
5. private HashMap cctld;
6.
7. public AssertTest()
8. {
9. cctld = new HashMap();
10. cctld.put("in", "India");
11. cctld.put("uk", "United Kingdom");
12. cctld.put("au", "Australia");
13. // more code...
14. }
15. // other methods ....
16. public String getCountry(String countryCode)
17. {
18. // What should be inserted here?
19. String country = (String)cctld.get(countryCode);
20. return country;
21. }
22. }
A. assert countryCode != null;
B. assert countryCode != null : "Country code can not be null" ;
C. assert cctld != null : "No country code data is available";
D. assert cctld : "No country code data is available";
E. assert cctld.size() != 0 : "No country code data is available";
Answer: C
Note: assertions should be used to check for conditions that should never happen.

** =================
4.7 Which of the following will print exactly 7?
1. class MyClass
2. {
3. public static void main(String args[])
4. {
5. double x = 6.5;
6. System.out.println(Math.floor(x+1));
7. System.out.println(Math.ceil(x));
8. System.out.println(Math.round(x));
9. }
10. }
A. Line 6 only
B. Lines 6 and 7 only
C. Lines 6, 7 and 8
D. None of these
Answer: D 8
Note: is double and output is 7.0

***** ========================
4.8 Consider the following statement:
Thread myThread = new Thread();
Which of the following statements are true regarding myThread?
A. The thread myThread is now in a runnable state.
B. The thread myThread has a priority of 5.
C. On calling the start() method on myThread, the run method in the Thread class will be executed.
D. On calling the start() method on myThread, the run method in the calling class will be executed.
Answer: C
Note: the priority of myThread will be inherited from the Thread that called the constructor.
haretu
 
Posts: 132
Joined: Sat Jan 08, 2011 9:56 pm

Re: jscp java certificate exam mock questions and answers

Postby haretu » Wed Jul 13, 2011 9:23 am

***** ========================
5.1 What will happen when you attempt to run the following code from command line using command – java AssertTest?
(Assume that the code is compiled with assertions enabled)
1. public class AssertTest
2. {
3. static int i = 10;
4. public static void main(String args[])
5. {
6. i = i*2;
7. try
8. {
9. assert isValid() : i = i/4;
10. }
catch(AssertionError ignore){}
11.
12. System.out.println("i = " +i);
13. }
14.
15. public static boolean isValid()
16. {
17. i = i * 2;
18. return false;
19. }
20. }
A. It will print - i = 20
B. It will print - i = 10
C. It will print - i = 40
D. It will print - i = 5
E. None of these
Answer: A
Note: It is not runtime Assertion enabled! And assert Expression1 cannot have side effect.


*** ===============
5.2 What will happen when you attempt to compile and run the following code?
1. public class WrapperDemo
2. {
3. public static void main(String args[])
4. {
5. Character c = new Character('A');
6. Integer i = new Integer('A');
7.
8. int a = c.intValue();
9. int b = i.intValue();
10.
11. if(a == b)
12. System.out.println("Both are equal");
13. else
14. System.out.println("Both are not equal");
15. }
16. }
A. It will print - Both are equal
B. It will print - Both are not equal
C. It will throw NumberFormatException at runtime
D. Compilation error at line 8
E. Compilation error at line 6
Answer: D
Note: The Wrapper class Boolean and Character do not extend java.lang.Number,and do not implement its intValue() method.


** =====================
5.3 Given a byte with a value of 01110111, which of the following statements will produce 00111011?
Note : 01110111 = 0x77
A. 0x77 << 1;
B. 0x77 >>> 1;
C. 0x77 >> 1;
D. B and C.
E. None of the above
Answer: D
Note: Since the leading bit(first bit from left) is a 0, both the >> and >>> operators work identically.


** ==================
5.4 A class with all its constructors as private can't be extended and instantiated by any other class. True/False?
A. True
B. False
Answer: A
Note: From the child class default constructor, there is a default call to the parent class default constructor.


*** =======================
5.5 Which of the following statements are correct?
A. Arithmetic promotion of object references requires explicit casting.
B. Both primitives and object references can be both converted and cast.// primitives is the base data types
C. Only primitives are converted automatically; to change the type of an object reference, you have to do a cast.
D. Only object references are converted automatically; to change the type of a primitive, you have to do a cast.
E. Casting of numeric types may require a runtime check.
Answer: B
Note: only the casting of object references may potentially require a runtime check.


***** ========================
5.6 Comment if the following code implements hashCode() method of Object class correctly? Select the most appropriate answer.
1. public class CorrectHashCode
2. {
3. private int number;
4.
5. public CorrectHashCode(int i)
6. {
7. number = i;
8. }
9.
10. public int hashCode()
11. {
12. int i = (int)(Math.random() * 100);
13. return i * number;
14. }
15.
16. // other methods
17. }
A. The code implements hashCode() method correctly
B. The code does not implement hashCode() method correctly
C. This implementation of hashCode() method is correct if only if the equals() method is not overridden
D. The given code does not contain enough information to comment on the correctness of the hashCode() method implementation
E. None of these
Answer: B


***** ====================
5.7 What will happen if you compile/run this code? Assume the code to be written inside main().
int i = 012;
int j = 034;
int k = 056;
int l = 078;//error, 八进制越过范围
System.out.println(i);
System.out.println(j);
System.out.println(k);
A. Prints 12, 34 and 56.
B. Prints 24, 68 and 112.
C. Prints 10, 28 and 46.
D. Compilation error.
Answer: D


**** =====================
5.8 Which of the following is a valid way to declare an abstract method which is intended to be public?
A. public abstract method();
B. public abstract void method();
C. public abstract void method(){}
D. public virtual void method();
E. public void method() implements abstract;
Answer: B
Note: the {} braces indicate that the method is actually implemented, and is therefore not abstract. In other words, even empty braces are a valid way to define a method.


** ====================
5.9 What is the effect of adding the sixth element to a Vector created using the following code?
new Vector(5, 10);
A. An IndexOutOfBounds exception is thrown.
B. The vector grows in size to a capacity of 10 elements.
C. The vector grows in size to a capacity of 15 elements.
D. Nothing. Size is the number of the elements in vector, while capacity will increase. The Vector will have grown when the fifth element was added, because Vector elements are zero-based.


*** ================
5.10 You have created a TimeOut class as an extension of Thread, the purpose being to print a"Time's Up" message if the Thread is not interrupted within 10 seconds of being started. Here is the run method which you have coded.
1. public void run()
2. {
3. System.out.println("Start!");
4. try
5. {
6. Thread.sleep(10000 );
7. System.out.println("Time's Up!");
8. }
9. catch(InterruptedException e)
10. {
11. System.out.println("Interrupted!");
12. }
13. }
Given that a program creates and starts a TimeOut object, which of the following statements is true?
A. Exactly 10 seconds after the start method is called, "Time's Up!" will be printed.
B. Exactly 10 seconds after "Start!" is printed, "Time's Up!" will be printed.
C. The delay between "Start!" being printed and "Time's Up!" will be 10 seconds plus or minus one tick of the system clock.
D. If "Time's Up!" is printed you can be sure at least 10 seconds have elapsed since "Start!" was printed.
Answer: D

* ===================
5.11 Which of the following are the correct and appropriate implementations of the hashCode() method that can be inserted at line 20 in this code;so that the code compiles correctly and the general contracts of equals and hashCode methods are observed.
1. public class CorrectHashCode
2. {
3. private int number;
4.
5. public CorrectHashCode(int i)
6. {
7. number = i;
8. }
9.
10. public boolean equals(Object obj)
11. {
12. if(obj instanceof CorrectHashCode)
13. {
14. return
15. (number == ((CorrectHashCode)obj).number);
16. }
17. return false;
18. }
19.
20. // which implementation of hashCode should be inserted here?
21. }
A. public int hashCode() {
Integer num = new Integer(number);
return num.hashCode();
}
B. public int hashCode(){
int i = (int)(Math.random() * 100);
return i * number;
}
C. public int hashCode(){
return number;
}
D. public long hashCode(){
Integer num = new Integer(number);
return num.hashCode();
}
Answer: AC
haretu
 
Posts: 132
Joined: Sat Jan 08, 2011 9:56 pm

Re: jscp java certificate exam mock questions and answers

Postby haretu » Wed Jul 13, 2011 9:24 am

6.1 You wish to store a small amount of data and make it available for rapid access. You do not have a need for the data to be sorted, uniqueness is not an issue and the data will remain fairly static Which data structure might be most suitable for this requirement?
A. TreeSet
B. HashMap
C. LinkedList
D. An array
Answer: D

*** ==================
6.2 What will happen when you attempt to compile and run the following code?
public class MyThread extends Thread {
String myName;
MyThread(String name){
myName = name;
}
public void run(){
for(int i=0; i<100;i++) {
System.out.println(myName);
}
}
public static void main(String args[]){
try {
MyThread mt1 = new MyThread("mt1");
MyThread mt2 = new MyThread("mt2");
mt1.start();
// XXX
mt2.start();
}
catch(InterruptedException ex){}
}
}
A. The above code in its current condition will not compile.
B. In order to make the MyThread class prints "mt1" (100 times) followed by "mt2" (100 times), mt1.join(); can be placed at //XXX position.
C. In order to make the MyThread class prints "mt1" (100 times) followed by "mt2" (100 times), mt1.sleep(100); can be placed at //XXX position.
D. In order to make the MyThread class prints "mt1" (100 times) followed by "mt2" (100 times), mt1.run(); can be placed at //XXX position.
E. In order to make the MyThread class prints "mt1" (100 times) followed by "mt2" (100 times), there is no need to write any code.
Answer: AB
Note: the above code will not compile as "InterruptedException" is never thrown in the try block.

**** =========================
6.3 The no-argument constructor provided by the compiler when no constructor is explicitly provided in the code is always public. True/False?
A. True
B. False
Answer: B
Note: The access modifier for the default constructor provided by the compiler is the same as that of the class to which the constructor belongs.

**** ========================
6.4 What will be output by the following statement?
System.out.println(1 << 32);
A. 1
B. -1
C. 32
D. -2147483648
Answer: A
Note: With the left shift operator the bits will "wrap around" i.e. the shift takes place by (32 % 32) = 0 bits. Thus the result of System.out.println(1 << 32) would be 1.

*** ====================
6.5 Which of the following can be applied to constructors?
A. final
B. static
C. synchronized
D. native
E. None of these
Answer: E
Note: a constructor cannot be synchronized as objects are owned by threads and never any two threads can share a single object.

*** ==================
6.6 Which of the following are valid statements regarding the following piece of code?
1. String s1 = "abcd";
2. StringBuffer sb1 = new StringBuffer("abcd");
3. int val = 6;
4. System.out.println(s1 + val);
5. System.out.println(sb1 + val);
A. The text "abcd6" is displayed followed by "abcd6".
B. The code fails to compile because String conversion does not apply to StringBuffer.
C. The code compiles but upon execution, throws a NullPointerException at line 5.
D. The code fails to compile at line 2, because this is not a valid way to create a StringBuffer.
Answer: B
Note: but "sb1 + s1" is OK.

**** ====================
6.7 Which of the following can be inserted at line 10, so that the code compiles and prints the required result.
public class WrapperDemo {
public static void main(String args[]) {
System.out.println(getArea(7));
}
public static Double getArea(int r) {
// what should be inserted here?
}
}
A. return Double.valueOf(Math.PI*r*r);
B. return Double.parseDouble(Math.PI*r*r);
C. return Double.parseDouble("" + Math.PI*r*r);
D. return new Double(Math.PI*r*r);
E. return Double.valueOf("" + Math.PI*r*r);
F. Only option D is correct
Answer: DE
Note: The static method valueOf(String s) takes a String parameter representing double value and returns a Double object holding this value.

**** =====================
6.8 Consider the class hierarchy shown below:
FourWheeler
(implements DrivingUtilities)
/ / \ \
/ / \ \
/ / \ \
/ / \ \
/ / \ \
Car Truck Bus Crane
Consider the following code below:
1. DrivingUtilities du;
2. FourWheeler fw = new FourWheeler();
3. Car myCar;
4. du = (DrivingUtilities)fw;
5. myCar = (Car)du;
Which of the statements below are true?
A. The code will compile and run.
B. The code will compile but will throw an exception at runtime because the runtime class of du cannot be converted to myCar, because going down the hierarchy is not allowed.
C. The code at line4 will compile even without the explicit cast.
D. Line 5 will not compile because an interface cannot be casted to a class.
Answer: Bc
Note: C is correct because at runtime the variable du holds a reference to an object of class FourWheeler which cannot be converted to an object of class myCar as it makes it go down the hierarchy which is illegal.

**** ====================
6.9 Which of the following is true regarding main Thread?
A. It is the thread from which other "child" threads will be spawned.
B. It must be the last thread to finish execution. When the main thread stops, the program terminates.
C. It has the highest priority.
D. main is not a thread.
Answer: AB
Note: First of all please note that main is a thread. By default it has normal priority of 5. You can check this by "Thread t = Thread.currentThread();" and then checking the priority by calling getPriority() method.

*** =======================
6.10 What will happen when you attempt to compile and run the following code?
1. public class Test
2. {
3. public static void main(String args[])
4. {
5. Boolean b1 = new Boolean("Yes");
6. Boolean b2 = new Boolean("No");
7. Object obj = new Boolean(b2.equals(b1));
8.
9. System.out.println(obj);
10. }
11. }
A. Compilation error at line 5
B. Compilation error at line 6
C. Compilation error at line 7
D. It will print - true
E. It will print - false
Answer: D

*** ===================
6.11 What will happen when you attempt to compile and run the following code?
(Assume that the code is compiled and run with assertions enabled)
1. public class AssertTest
2. {
3. public static void main(String args[])
4. {
5. int i = 5;
6. Object obj1 = new Object();
7. Object obj2 = obj1;
8.
9. try
10. {
11. assert obj1.equals(obj2) : i += 10;
12. }
13. catch(AssertionError ignore){}
14. System.out.println(i);
15. }
16. }
A. Compilation error at line 11
B. Compilation error at line 13
C. It will print-10
D. It will print-15
E. It will print-5
Answer: E
Note; can catch AssertionError
haretu
 
Posts: 132
Joined: Sat Jan 08, 2011 9:56 pm

Re: jscp java certificate exam mock questions and answers

Postby haretu » Wed Jul 13, 2011 9:25 am

*** ================
7.1 What will happen when you attempt to compile and run the following code?
1. public class TestEqual
2. {
3. public static void main(String args[])
4. {
5. Boolean bool[] = new Boolean[5];
6. bool[0] = new Boolean("true");
7. bool[1] = new Boolean(false);
8.
9. System.out.print(bool[0].equals(bool[3]));
10. System.out.print(" " + bool[1].equals(bool[3]));
11. }
12. }
A. The code will not compile
B. It will throw NullPointerException at runtime
C. It will print - false false
D. It will print - false true
E. It will print - true false
Answer: C

***** =====================
7.2 What will happen when you compile and run the following code?
public class MyClass{
private static int x = getValue();
private static int y = 5;
private static int getValue() {
return y;
}
public static void main(String args[]) {
System.out.println(x);
}
}
A. Compiler error complaining about access restriction of private variables of MyClass.
B. Compiler error complaining about forward referencing.
C. prints 0
D. prints 10
Answer: C
Note: as y = 5 is not called before "x = getValue()".

***** =================
7.3 If a.equals(b) is true, which of the following statements are always true?
(Assume that the classes of a and b have overridden all the required methods correctly)
A. b.equals(a) is always true
B. a == b is always true
C. a.getClass() == b.getClass() is always true
D. a.hashCode() == b.hashCode() is always true
E. Only option A is correct
Answer: AD
Note: The general contract of hashCode() method of Object class mentions : If two objects are equal according to the equals(Object) method, then calling the hashCode method on each of the two objects must produce the same integer result.
If you are designing a class that overrides equals method, it is generally necessary to override the hashCode method as well, so as to maintain the general contract for the hashCode method, which states that equal objects must have equal hash codes.
Note that converse of this is not correct. i.e. if a.equals(b) is false, the hash codes returned by them need not be different.

** ==================
7.4 Which of the following keywords can be applied to the variables or methods of an interface?
A. static
B. private
C. synchronized
D. protected
E. public
Answer: AE

*** =====================
7.5 What is displayed when the following code fragment is compiled and executed (assume that the enveloping class and method is correctly declared and defined):
StringBuffer sb1 = new StringBuffer("abcd");
StringBuffer sb2 = new StringBuffer("abcd");
String s1 = new String("abcd");
String s2 = "abcd";
System.out.println(s1==s2);
System.out.println(s1=s2);
System.out.println(sb1==sb2);
System.out.println(s1.equals(sb1));
System.out.println(sb1.equals(sb2));
A. The code fails to compile, complaining that the line System.out.println(s1=s2); is illegal.
B. The code fails to compile because the equals() method is not defined for the StringBuffer class.
C. false true true false false
D. false abcd false false false
E. false true false false false
Answer: D
Note: StringBuffer does not override the equals() method, and so will always only compare the object references.

*** ====================
7.6 The Class class has no public constructor.
A. True
B. False
Answer: A
Note: Class has no public constructor. Instead Class objects are constructed automatically by the Java Virtual Machine as classes are loaded and by calls to the defineClass method in the class loader.

***** =========================
7.7 What will be printed by executing the following code?
1. String s1 = new String("Case Matters");
2. String s2 = new String("case matters");
3. String s3 = s1.toLowerCase();
4. String s4 = s2.toLowerCase();
5. System.out.print((s1 == s3) + " ");
6. System.out.print((s2 == s4) + " ");
7. System.out.print((s3 == s4) + " ");
A. false false false
B. false true true
C. true true true
D. true false true
E. None of these
Answer: E
Note: When changing the case of strings the rule is that the original string is returned if there is no change required otherwise a new string object is created.

*** ========================
7.8 What results from the following code fragment?
1. int i = 5;
2. int j = 10;
3. System.out.println(i + ~j);
A. Compilation error because "~" doesn't operate on integers
B. -5
C. -6
D. 15
Answer: C
Note: Java allows you to use ~ operator for integer type variables and The shortest way to calculate is ~j = (- j) - 1
haretu
 
Posts: 132
Joined: Sat Jan 08, 2011 9:56 pm

Next

Return to JAVA

Who is online

Users browsing this forum: Yahoo [Bot] and 1 guest

cron