Language Fundamentals 3 Set 2

  • Uploaded by: rahul rastogi
  • 0
  • 0
  • June 2020
  • PDF

This document was uploaded by user and they confirmed that they have the permission to share it. If you are author or own the copyright of this book, please report to us by using this DMCA report form. Report DMCA


Overview

Download & View Language Fundamentals 3 Set 2 as PDF for free.

More details

  • Words: 1,705
  • Pages: 10
Language Fundamentals Question 1 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 1 b e g h k goto new finally const do

Question 2 class MCZ15 { public static void main float a = 1.1e1f; // float b = 1e-1F; // float c = .1e1f; // double d = .1d; // double e = 1D; // }}

(String[] args) { 1 2 3 4 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 2 f None of Floating-point literals are covered in section 3.10.2 of the JLS. A the above 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.

Question 3 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 3 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 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. b. c. d. e.

1 2 3 4 None of the above

ANSWER The literal 1.0 is a double and can not be used to initialize a float without 4 d 4 an explicit cast.

Question 5 Which of these words belong to the set of Java keywords? a. b. c. d. e. f. g. h. i. j. k.

next catch function instanceof mod const or Boolean goto import transient ANSWER 5 b d f i j k catch instanceof const goto import transient

Question 6 class MCZ17 { public static void main String a = “\n”; String b = “\r”; String c = “\u000a”; String d = “\u000d”; }}

(String[] args) { // 1 // 2 // 3 \u000a = new line // 4 \u000d = return

Compile-time errors are generated at which lines? a. b. c. d.

1 2 3 4 ANSWER 6 c 3 The compiler interprets \u000a as a line terminator. The escape sequence

d 4

\n should be used instead. Similarly, \u000d is interpreted as a line terminator. The escape sequence \r should be used instead.

Question 7 Which of these words belong to the set of Java keywords? a. b. c. d. e. f. g. h. i. j. k. l. m. n.

byte short int long decimal int64 float single double boolean char unsigned array string ANSWER 7 a b c d g i j k byte short int long float double boolean char

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. b. c. d. e. f.

1 2 3 4 5 None of the above

ANSWER All of the declarations are legal. String b is a single quote followed None of by the letter A followed by another single quote. String c is the 8 f the letter A. String d is the Unicode character that is represented by the above hexadecimal value D7AF. String literals are covered in section 3.10.5 of the JLS.

Question 9 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 9 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. b. c. d. e.

String a = 'a'; String b = 'abc'; String c = '\u0041'; String d = '\uabcd'; None of the above ANSWER

10 e

None of the above

String literals are declared using double quotes, but all of the declarations here use single quotes.

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. b. c. d. e.

char a = a; char b = abc; char c = \u0041; char d = \uabcd; None of the above ANSWER Unicode char literals are declared using single quotes, but none of None of the declarations here use single quotes. The declaration of char b, 11 e the above is also problematic, because it contains more than one char.

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. b. c. d. e. f.

1 2 3 4 5 None of the above ANSWER

All of the declarations are legal. The first three ( 061, '\61', None of '\061' ) are declared in octal format. The fourth (0x0031) is 12 f the above declared as a hexadecimal literal. The fifth ('\u0031') is a Unicode escape sequence.

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. b. c. d. e.

1 2 3 4 5 ANSWER 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 d 4 13 in a compile-time error. Java does not accept the hexadecimal escape e 5 sequences of the C programming language. However, Java does accept Unicode escapes (JLS 3.3).

Question 14 class JJF6 { char c1 = 0xffff; char c2 = 0xfffff; byte b1 = 0xffff; byte b2 = 0x7f; byte b3 = 0xff; byte b4 = -0x80; }

// // // // // //

1 2 3 4 5 6

Compile-time errors are generated at which lines? a. 1 b. 2

c. d. e. f.

3 4 5 6 ANSWER b 2 14 c 3 e 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.

Question 15 class MCZ19 { public static String a1 = String b1 = String c1 = String d1 = }}

void main (String[] args) { null; // 1 'null'; // 2 "null"; // 3 "'null'"; // 4

A compile-time error is generated at which line? a. b. c. d. e.

1 2 3 4 None of the above 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 15 b 2 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

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. b. c. d. e. f.

char a = 0x0041; char b = '\u0041'; char c = 0101; char d = -1; char e = (char)-1; None of the above ANSWER The assignment of -1 to char d generates a compile-time error, char d because the primitive char type is unsigned. A negative int can not 16 d = -1; 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.

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. 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 There are two primitive boolean values: true and false. boolean b1 Both must be written with lower case letters. Although the C 17 a = true; programming language accepts zero as a boolean value, the Java programming language does not.

Question 18 class GFM17 { int x; public static void main(String[] args) {

// 1 // 2

}}

int y = 0; System.out.print(x+","); System.out.print(y);

// 3 // 4 // 5

What is the result of attempting to compile and run the program? a. b. c. d. e. f. g. h. i.

Prints: 0,0 Compile-time error at line 1. Compile-time error at line 1. Compile-time error at line 2. Compile-time error at line 3. Compile-time error at line 4. Compile-time error at line 5. Run-time error None of the above ANSWER Anytime a field is accessed from within a static context it is very Compile-time important to verify that the field is also static. If the field is 18 f error at line 4. instead an instance variable then the result is a Compile-time error.

Related Documents


More Documents from ""