You are on page 1of 11

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.  Compile-time error at line 1.


b.  Compile-time error at line 2.
c.  Compile-time error at line 3.
d.  An attempt to run GRC7 from the command line fails.
e.  An attempt to run GRC8 from the command line fails.
f.  An attempt to run GRC9 from the command line fails.

ANSWER
An attempt to run GRC7 from the Section 12.1.4 of the JLS requires the
d  command line fails.  An attempt to main method to be declared static. In this
1 e  run GRC8 from the command line example, each of the three main methods
f  fails.  An attempt to run GRC9 from are not declared static. The result is an
the command line fails.  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.  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
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.  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
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.  qualified
b.  record
c.  repeat
d.  restricted
e.  label
f.  to
g.  type
h.  until
i.  value
j.  virtual
k.  xor
l.  None of the above
ANSWER

None of the All of these are keywords of the Pascal programming language,
4 l 
above  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.  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
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.  exit
b.  strictfp
c.  enum
d.  super
e.  abort
f.  event
g.  goto
h.  native
i.  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.  Prints nothing.


b.  Prints: null
c.  Compile-time error
d.  Run-time error
e.  None of the above

ANSWER
Prints: The System.out.print method prints the word null if the argument is a
7 b 
null  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.  Prints: 12
b.  Prints: 15
c.  Prints: 18
d.  Prints: 1,4,7
e.  Prints: 2,5,8
f.  Prints: 3,6,9
g.  Compile-time error
h.  Run-time error
i.  None of the above

ANSWER
Arrays a1, a2 and a3 all contain 3 integers. The first element of the
Prints:
8 e  array has an index of 0 so an index of one refers to the second element
2,5,8 
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.  Prints: f,ff,7f


b.  Prints: f,ff,ff
c.  Prints: 7f,ffff,7fff
d.  Prints: ff,ffff,ffff
e.  Prints: 7fff,ffffff,7fffff
f.  Prints: ffff,ffffff,ffffff
g.  Compile-time error
h.  Run-time error
i.  None of the above

ANSWER
9 c  Prints: A byte is an 8 bit signed value. A char is a 16 bit unsigned value. A
7f,ffff,7fff  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.  

Question 10
class MWC104 {
public static void main(String[] args) {
int[5] a1; // 1
int []a2; // 2
int[ ]a3; // 3
int a4[]; // 4
}}

A compile-time error is generated at which line?

a.  1
b.  2
c.  3
d.  4
e.  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.  Prints: 0000,ffff


b.  Prints: 00000000,ffffffff
c.  Prints: 7fff,8000
d.  Prints: 8000,7fff
e.  Prints: 7fffffff,80000000
f.  Prints: 80000000,7fffffff
g.  Compile-time error
h.  Run-time error
i.  None of the above

ANSWER
An int is a 32 bit signed value. The left most bit is the sign
Prints:
11 f  bit. The sign bit is zero for positive numbers and one for
80000000,7fffffff 
negative numbers.  

Question 12
Which of the following represent the full range of type char?

a.  '\u0000' to '\u7fff'


b.  '\u0000' to '\uffff'
c.  0 to 32767
d.  0 to 65535
e.  -32768 to 32767
f.  -65536 to 65535

ANSWER
'\u0000' to A char is a 16 bit unsigned value; so none of the char values

12 '\uffff'  0 to are negative and the minimum value is zero. The maximum

65535  value is 2 - 1.  
16

Question 13
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
13 b  0x0000 to 0xffff  0 to A char is a 16 bit unsigned value; so none of the char
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.  Prints: 0,0,0,0,0,null


b.  Prints: 0,0,0,0,0,
c.  Prints: 0,0, ,0,0,
d.  Compile-time error
e.  Run-time error
f.  None of the above

ANSWER
Prints: The default value of type char is the null character. When it is
14 a 
0,0,0,0,0,null  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.  Prints: 0,0,0,false,null


b.  Prints: 0,0,0,false,
c.  Prints: 0,0.0,0.0,false,null
d.  Prints: 0,0.0,0.0,false,
e.  Prints: 0,0.0,0.0,true,null
f.  Prints: 0,0.0,0.0,true,
g.  Prints: 0,0,0,true,null
h.  Prints: 0,0,0,true,
i.  Compile-time error
j.  Run-time error
k.  None of the above

ANSWER
Generally speaking, numeric type variables are initialized
Prints:
15 c  to zero. Primitive boolean variables are initialized to
0,0.0,0.0,false,null 
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.  Prints: true,true,true


b.  Prints: false,false,false
c.  Prints: null,null,null
d.  Prints: false,true,false
e.  Compile-time error
f.  Run-time error
g.  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, false, of
Compile- the array type, boolean[], even though the array is declared locally.
16 e 
time error  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
Local variables are not initialized automatically, and must be
Compile- initialized explicitly before attempting to access the value. The
17 c 
time error  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.  abstract, default, if, private, this


b.  do, implements, protected, boolean, throw
c.  case, extends, int, short, try
d.  import, break, double, exception, throws
e.  byte, else, instanceof, return, transient
f.  None of the above

ANSWER
import, break, double, The word exception is not a Java keyword. The words
18 d 
exception, throws  import, break, double and throws are Java keywords.  

Question 19
Which of these lists contains at least one word that is not a Java keyword?

a.  interface, static, void, catch, final


b.  char, strictfp, finally, long, volatile
c.  native, super, class, float, while
d.  const, for, new, switch, import
e.  continue, finalize, goto, package, synchronized
f.  None of the above
ANSWER
continue, finalize, The word finalize is the name of a method of the Object
19 e  goto, package, class: It is not a keyword. The words continue, goto,
synchronized  package and synchronized are all Java keywords.  

You might also like