Vous êtes sur la page 1sur 15

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.  1
b.  2
c.  3
d.  4
e.  5
f.  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 d.  super g.  goto


b.  strictfp e.  abort h.  native
c.  enum f.  event i.  exception
Answer:
b  d  g  h  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:
Prints: The System.out.print method prints the word null if the argument is a String

null  reference that is null.  

Question 4
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:
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.  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 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.  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 word
g  i  extends  break  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:
d  4  The literal 1.0 is a double and can not be used to initialize a float without an explicit cast.  

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

a.  next e.  mod i.  goto


b.  catch f.  const j.  import
c.  function g.  or k.  transient
d.  instanceof h.  Boolean
Answer:
b  d  f  i  j  k  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 \r
d  4 
should be used instead.  

Question 10
Which of these words belong to the set of Java keywords?
a.  byte e.  decimal i.  double m.  array
b.  short f.  int64 j.  boolean n.  string
c.  int g.  float k.  char
d.  long h.  single l.  unsigned
Answer:
a  b  c  d  g  i  j  k  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.  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 the letter A
None of
followed by another single quote. String c is the letter A. String d is the Unicode
f  the
character that is represented by the hexadecimal value D7AF. String literals are
above 
covered in section 3.10.5 of the JLS.  

Question 12
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:
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:
None of the String literals are declared using double quotes, but all of the declarations

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
e  declarations here use single quotes. The declaration of char b, is also problematic,
the above 
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.  1 d.  4
b.  2 e.  5
c.  3 f.  None of the above
Answer:
All of the declarations are legal. The first three ( 061, '\61', '\061' ) are declared in
None of
f  octal format. The fourth (0x0031) is declared as a hexadecimal literal. The fifth
the above 
('\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.
e  5  Java 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 negative
d  0 to 65535  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.  -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
Answer:
A char is a 16 bit unsigned value; so none of the char values are
b  d  0x0000 to 0xffff  0 to
negative and the minimum value is zero. The maximum value is 2 16

f  0177777  0 to 65535 
- 1.  

Question 19
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 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 hex
b  2  value 0xff is of type int, and the decimal value is 255. The maximum positive value of
c  3  type byte is 127. The value 255 is beyond the range of type byte; so 255 can not be
e  5  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
b.  2 d.  4 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 c1 is the word null. String d1 is a single
b  2 
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

-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.  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 must be written
boolean b1 =
a  with lower case letters. Although the C programming language accepts zero
true; 
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.  Prints: 0
b.  Prints: 1
c.  Compile-time error
d.  Run-time error
e.  None of the above
Answer:
Local variables are not initialized automatically, and must be initialized
Compile-
c  explicitly before attempting to access the value. The local variable i3 will not be
time error 
initialized if i1 is less than or equal to zero; so the result is a compile-time error.

Question 24
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 d.  Compile-time error at line 4.
b.  Compile-time error at line 2. g.  Compile-time error at
line 1. e.  Compile-time error at line 5.
c.  Compile-time error at line 3. 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
Compile-time
f  to 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.  

Vous aimerez peut-être aussi