Language Fundamentals 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. b. c. d. e. f.
Compile-time error at line 1. Compile-time error at line 2. Compile-time error at line 3. 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. ANSWER An attempt to run GRC7 from the d command line fails. An attempt to 1 e run GRC8 from the command line f 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.
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. b. c. d. e. f.
1 2 3 4 5 None of the above
ANSWER The escape sequences are as follows: '\b' (backspace), '\f' (formfeed), '\n' (newline), '\r' (carriage return), '\t' (horizontal tab), '\\' (backslash), '\"' (double 2 a 1 quote), '\'' (single quote). Yes, you must memorize the escape sequences! Just remember "big farms need red tractors".
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. b. c. d. e. f.
1 2 3 4 5 None of the above ANSWER The escape sequences are as follows: '\b' (backspace), '\f' (formfeed), '\n' (newline), '\r' (carriage return), '\t' (horizontal tab), '\\' (backslash), '\"' (double 3 c 3 quote), '\'' (single quote). Yes, you must memorize the escape sequences! Just remember "big farms need red tractors".
Question 4 Which of these words belongs to the set of Java keywords? a. b. c. d. e. f. g. h.
qualified record repeat restricted label to type until
i. j. k. l.
value virtual xor None of the above ANSWER 4 l
None of the above
All of these are keywords of the Pascal programming language, but none are Java keywords
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. b. c. d. e. f.
1 2 3 4 5 None of the above ANSWER The escape sequences are as follows: '\b' (backspace), '\f' (formfeed), '\n' (newline), '\r' (carriage return), '\t' (horizontal tab), '\\' (backslash), '\"' (double 5 e 5 quote), '\'' (single quote). Yes, you must memorize the escape sequences! Just remember "big farms need red tractors".
Question 6 Which of these words belong to the set of Java keywords? a. b. c. d. e.
exit strictfp enum super abort
f. g. h. i.
event goto native exception ANSWER 6 b d g h strictfp super goto native
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. b. c. d. e.
Prints nothing. Prints: null Compile-time error Run-time error None of the above ANSWER Prints: 7 b null
The System.out.print method prints the word null if the argument is a String reference that is null.
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. b. c. d.
Prints: 12 Prints: 15 Prints: 18 Prints: 1,4,7
e. f. g. h. i.
Prints: 2,5,8 Prints: 3,6,9 Compile-time error Run-time error None of the above ANSWER Prints: 8 e 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.
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. b. c. d. e. f. g. h. i.
Prints: f,ff,7f Prints: f,ff,ff Prints: 7f,ffff,7fff Prints: ff,ffff,ffff Prints: 7fff,ffffff,7fffff Prints: ffff,ffffff,ffffff Compile-time error Run-time error None of the above ANSWER
9 c
Prints: 7f,ffff,7fff
Question 10
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.
class MWC104 { public static int[5] a1; int []a2; int[ ]a3; int a4[]; }}
void main(String[] args) { // 1 // 2 // 3 // 4
A compile-time error is generated at which line? a. b. c. d. e.
1 2 3 4 None of the above ANSWER 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 10 a 1 the array. Instead, the dimension expression should be contained in an array creation expression such as new int[5].
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. b. c. d. e. f. g. h. i.
Prints: 0000,ffff Prints: 00000000,ffffffff Prints: 7fff,8000 Prints: 8000,7fff Prints: 7fffffff,80000000 Prints: 80000000,7fffffff Compile-time error Run-time error None of the above ANSWER 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.
Question 12 Which of the following represent the full range of type char? a. b. c. d. e. f.
'\u0000' to '\u7fff' '\u0000' to '\uffff' 0 to 32767 0 to 65535 -32768 to 32767 -65536 to 65535 ANSWER '\u0000' to b 12 '\uffff' 0 to d 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 2 - 1. 16
Question 13 Which of the following represent the full range of type char? a. b. c. d. e. f. g.
-0x8000 to 0x7fff 0x0000 to 0xffff -0100000 to 077777 0 to 0177777 0 to 32767 0 to 65535 -32768 to 32767 ANSWER 13
A char is a 16 bit unsigned value; so none of the char b 0x0000 to 0xffff 0 to values are negative and the minimum value is zero. The d f 0177777 0 to 65535 maximum value is 2 - 1. 16
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. b. c. d. e. f.
Prints: 0,0,0,0,0,null Prints: 0,0,0,0,0, Prints: 0,0, ,0,0, Compile-time error Run-time error None of the above ANSWER Prints: 14 a 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.
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. b. c. d. e. f. g. h. i. j. k.
Prints: 0,0,0,false,null Prints: 0,0,0,false, Prints: 0,0.0,0.0,false,null Prints: 0,0.0,0.0,false, Prints: 0,0.0,0.0,true,null Prints: 0,0.0,0.0,true, Prints: 0,0,0,true,null Prints: 0,0,0,true, Compile-time error Run-time error None of the above ANSWER 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.
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. b. c. d. e. f. g.
Prints: true,true,true Prints: false,false,false Prints: null,null,null Prints: false,true,false Compile-time error Run-time error None of the above ANSWER Variable b1 is initialized to false, because it is a class member. The array component array[0] is initialized to the default value, Compile- false, of the array type, boolean[], even though the array is 16 e time error 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.
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 ANSWER 17 c
Compiletime 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.
Question 18 Which of these lists contains at least one word that is not a Java keyword? a. b. c. d. e. f.
abstract, default, if, private, this do, implements, protected, boolean, throw case, extends, int, short, try import, break, double, exception, throws byte, else, instanceof, return, transient None of the above ANSWER The word exception is not a Java keyword. The import, break, double, 18 d words import, break, double and throws are exception, throws Java keywords.
Question 19 Which of these lists contains at least one word that is not a Java keyword? a. b. c. d. e. f.
interface, static, void, catch, final char, strictfp, finally, long, volatile native, super, class, float, while const, for, new, switch, import continue, finalize, goto, package, synchronized None of the above ANSWER The word finalize is the name of a method of the continue, finalize, Object class: It is not a keyword. The words 19 e goto, package, continue, goto, package and synchronized are synchronized all Java keywords.