Vous êtes sur la page 1sur 10

Language Fundamentals

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
ANSWER
1 b  e  g  h  k  goto  new  finally  const  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
ANSWER
Floating-point literals are covered in section 3.10.2 of the JLS. A
None of floating-point literal can begin with either a digit or a decimal point.
2 f 
the above  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.  declare
b.  global
c.  const
d.  preserve
e.  continue
f.  Float
g.  extends
h.  select
i.  break
j.  dim
k.  instanceOf

ANSWER
c  e  const  continue  All of the letters of all Java keywords are lower case. The
3
g  i  extends  break  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.  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
4 d  4 
explicit cast.  

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

ANSWER
5 b  d  f  i  j  k  catch  instanceof  const  goto  import  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
ANSWER
The compiler interprets \u000a as a line terminator. The escape sequence \n
c  3 
6 should be used instead. Similarly, \u000d is interpreted as a line terminator.
d  4 
The escape sequence \r should be used instead.  

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
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.  1
b.  2
c.  3
d.  4
e.  5
f.  None of the above

ANSWER
All of the declarations are legal. String b is a single quote followed by
None of
the letter A followed by another single quote. String c is the letter A.
8 f  the
String d is the Unicode character that is represented by the hexadecimal
above 
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.  clause
b.  loop
c.  expression
d.  overrides
e.  statement
f.  assertion
g.  validate
h.  exception
i.  cast
j.  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.  String a = 'a';


b.  String b = 'abc';
c.  String c = '\u0041';
d.  String d = '\uabcd';
e.  None of the above
ANSWER
None of the String literals are declared using double quotes, but all of the
10 e 
above  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.  char a = a;
b.  char b = abc;
c.  char c = \u0041;
d.  char d = \uabcd;
e.  None of the above

ANSWER
Unicode char literals are declared using single quotes, but none of
None of
11 e  the declarations here use single quotes. The declaration of char b, is
the above 
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.  1
b.  2
c.  3
d.  4
e.  5
f.  None of the above
ANSWER
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
12 f 
the above  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.  1
b.  2
c.  3
d.  4
e.  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; // 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

ANSWER
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
b  2 
maximum positive value of type byte is 127. The value 255 is beyond the
14 c  3 
range of type byte; so 255 can not be assigned to type byte without an
e  5 
explicit cast. The assignment expression b3 = (byte)0xff would assign the
value -1 to variable b3.  

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
ANSWER
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

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

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 be
16 d 
-1;  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.  boolean b1 = true;


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

ANSWER
There are two primitive boolean values: true and false. Both
boolean b1 = 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; // 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

ANSWER
Anytime a field is accessed from within a static context it is
Compile-time very 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.  

Vous aimerez peut-être aussi