LANGIUAGE FUNDAMENTAL
Question 1 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 quote), '\'' (single quote). e 5 Yes, you must memorize the escape sequences! Just remember "big farms need red tractors".
Question 2 Which of these words belong to the set of Java keywords? a. exit b. strictfp c. enum Answer: b d g h
d. super e. abort f. event
g. goto h. native i. exception
strictfp super goto native
Question 3 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. c. Compile-time error e. None of the above b. Prints: null d. Run-time error Answer: The System.out.print method prints the word null if the argument is a Prints: b null String reference that is null.
Question 4 Which of these words belong to the set of Java keywords? a. b. c. d. e. f. g. h. i. j. k.
Double Goto Min Extern New Signed Finally Const And Array Do
Answer: b e g h k
goto new finally const do
Question 5 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. b. c. d. e. f.
1 2 3 4 5 None of the above
Answer: Floating-point literals are covered in section 3.10.2 of the JLS. A floating-point None of f literal can begin with either a digit or a decimal point. Optionally, it can have a the above fractional part, an exponent part and a floating point suffix--f, F, d, or D.
Question 6 Which of these words belong to the set of Java keywords? a. b. c. d. e. f. g. h. i. j. k.
declare global const preserve continue Float extends select break dim instanceOf
Answer: c e const continue g i extends break
All of the letters of all Java keywords are lower case. The word instanceof is a Java keyword, but instanceOf is not.
Question 7 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 Answer: The literal 1.0 is a double and can not be used to initialize a float without an explicit d 4 cast.
Question 8 Which of these words belong to the set of Java keywords? a. next b. catch c. function d. instanceof Answer: b d f i j k
e. f. g. h.
mod const or Boolean
i. goto j. import k. transient
catch instanceof const goto import transient
Question 9 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
Answer: The compiler interprets \u000a as a line terminator. The escape sequence \n should be c 3 used instead. Similarly, \u000d is interpreted as a line terminator. The escape sequence d 4 \r should be used instead.
Question 10 Which of these words belong to the set of Java keywords?
a. byte b. short c. int d. long Answer: a b c d g i j k
e. f. g. h.
decimal int64 float single
i. j. k. l.
double boolean char unsigned
byte short int long float double boolean char
Question 11 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. b. c. d. e. f.
1 2 3 4 5 None of the above
m. array n. string
Answer: All of the declarations are legal. String b is a single quote followed by the letter None of A followed by another single quote. String c is the letter A. String d is the f the above Unicode character that is represented by the hexadecimal value D7AF. String literals are covered in section 3.10.5 of the JLS.
Question 12 Which of these words belongs to the set of Java keywords? a. b. c. d. e. f. g. h. i. j.
clause loop expression overrides statement assertion validate exception cast None of the above
Answer: j None of the above
Question 13 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'; c. String c = '\u0041'; e. None of the above b. String b = 'abc'; d. String d = '\uabcd'; Answer: String literals are declared using double quotes, but all of the declarations None of the e above here use single quotes.
Question 14 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; c. char c = \u0041; e. None of the above b. char b = abc; d. char d = \uabcd; Answer: Unicode char literals are declared using single quotes, but none of the None of the declarations here use single quotes. The declaration of char b, is also e above problematic, because it contains more than one char.
Question 15 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. b. c. d. e.
1 2 3 4 5
f. None of the above
Answer: f
All of the declarations are legal. The first three ( 061, '\61', '\061' ) are None of declared in octal format. The fourth (0x0031) is declared as a hexadecimal literal. the above The fifth ('\u0031') is a Unicode escape sequence.
Question 16 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 c. 3 e. 5 b. 2 d. 4 Answer: All of the escape sequences used in this question are defined for the C programming d 4 language. Those that are not also Java escape sequences result in a compile-time error. Java e 5 does not accept the hexadecimal escape sequences of the C programming language. However, Java does accept Unicode escapes (JLS 3.3).
Question 17 Which of the following represent the full range of type char? a. '\u0000' to '\u7fff' c. 0 to 32767 e. -32768 to 32767 b. '\u0000' to '\uffff' d. 0 to 65535 f. -65536 to 65535 Answer: b '\u0000' to '\uffff' A char is a 16 bit unsigned value; so none of the char values are d 0 to 65535 negative and the minimum value is zero. The maximum value is 2 - 1. 16
Question 18 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: b d 0x0000 to 0xffff 0 to f 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 2 1. 16
Question 19 class JJF6 { char c1 = 0xffff; // char c2 = 0xfffff; // byte b1 = 0xffff; // byte b2 = 0x7f; // byte b3 = 0xff; // byte b4 = -0x80; // } Compile-time errors are
1 2 3 4 5 6 generated at which lines?
a. 1 c. 3 e. 5 b. 2 d. 4 f. 6 Answer: The maximum char value is 0xffff. The maximum byte value is 127 = 0x7f. The b 2 hex value 0xff is of type int, and the decimal value is 255. The maximum positive c 3 value of type byte is 127. The value 255 is beyond the range of type byte; so 255 can e 5 not be assigned to type byte without an explicit cast. The assignment expression b3 = (byte)0xff would assign the value -1 to variable b3.
Question 20 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 c. 3 e. None of the above b. 2 d. 4 Answer: 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 b 2 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.
Question 21 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
Answer: The assignment of -1 to char d generates a compile-time error, because the char d primitive char type is unsigned. A negative int can not be assigned to a char d = -1; without an explicit cast. If the literal value -1 were cast to type char then the result would be \uffff.
Question 22 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. b. c. d. e. f.
boolean b1 = true; boolean b2 = TRUE; boolean b3 = 'true'; boolean b4 = "TRUE"; boolean b5 = 0; None of the above
Answer: a
boolean b1 There are two primitive boolean values: true and false. Both must be written with lower case letters. Although the C programming language accepts = true; zero as a boolean value, the Java programming language does not.
Question 23 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. b. c. d. e.
Prints: 0 Prints: 1 Compile-time error Run-time error None of the above
Answer: 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 24 class GFM17 { int x; public static void main(String[] args) { int y = 0; System.out.print(x+","); System.out.print(y); }} What is the result of attempting to compile
// // // // //
1 2 3 4 5
and run the program?
a. Prints: 0,0 d. Compile-time error at line 4. line 2. b. Compile-time error at g. Compile-time error at line 1. e. Compile-time error at line 5. line 3. c. Compile-time error at h. Run-time error line 1. f. Compile-time error at i. None of the above Answer: Anytime a field is accessed from within a static context it is very important to Compile-time f verify that the field is also static. If the field is instead an instance variable error at line 4. then the result is a Compile-time error.