Followers

Sunday, 5 August 2012

Question return types


Question 1

class JSC201 {
  static byte m1() {
    final char c1 = '\u0001';
    return c1;                                // 1
  }
  static byte m2(final char c2) {return c2;}  // 2
  public static void main(String[] args) {
    char c3 = '\u0003'; 
    System.out.print(""+m1()+m2(c3));         // 3
}}
What is the result of attempting to compile and run the program?
a.   Prints: 13
b.  Prints: 4
c.   Compile-time error at 1
d.  Compile-time error at 2
e.   Run-time error
f.   None of the above

Question 2

class JSC202 {
  static byte m1() {final short s1 = 2; return s1;}  // 1 
  static byte m2(final short s2) {return s2;}        // 2
  public static void main(String[] args) {
    short s3 = 4;
    System.out.print(""+m1()+m2(s3));                // 3
}}
What is the result of attempting to compile and run the program?
a.   Prints: 24
b.  Prints: 6
c.   Compile-time error at 1.
d.  Compile-time error at 2.
e.   Run-time error
f.   None of the above

Question 3

class JSC203 {
  static int m1(byte b) {return b;}   // 1
  static int m2(char c) {return c;}   // 2
  static int m3(long l) {return l;}   // 3
  public static void main(String[] args) {
    byte b = 1; char c = '\u0002'; long l = 4L;
    System.out.print(""+m1(b)+m2(c)+m3(l));
}}
What is the result of attempting to compile and run the program?
a.   Prints: 124
b.  Prints: 7
c.   Compile-time error at 1.
d.  Compile-time error at 2.
e.   Compile-time error at 3.
f.   Run-time error

Question 4

class JSC204 {
  static int m1(short s)  {return s;}   // 1
  static int m2(float f) {return f;}    // 2
  public static void main(String[] args) {
    short s = 3; float f = 5.0f;
    System.out.print(""+m1(s)+m2(f));
}}
What is the result of attempting to compile and run the program?
a.   Prints: 35.0
b.  Prints: 8.0
c.   Compile-time error at 1.
d.  Compile-time error at 2.
e.   Run-time error
f.   None of the above

Question 5

class JSC205 {
  static int m1(int i) {return i;}     // 1
  static void m2(int i) {return i;}    // 2
  static int m3(int i) {return;}       // 3
  public static void main(String[] args) {
    System.out.print(""+m1(1)+m2(2)+m3(3)); // 4
}}
What is the result of attempting to compile and run the program?
a.   Prints: 123
b.  Prints: 6
c.   Compile-time error at 1.
d.  Compile-time error at 2.
e.   Compile-time error at 3.
f.   Compile-time error at 4.





 Answers




No.
Answer
Remark
1
Compile-time error at 2 
There is a compile-time error at 2. The char type variable c2 is not a compile-time constant, so it can not be assigned to type byte without an explicit cast. The method parameter c2 is declared final, so the value of c2 can not be changed within method m2. The value of method parameter c2 is set at run time to the value of the argument that is provided when m2 is invoked at line 3. For that reason, the method parameter c2 is not a compile-time constant. In method m2, the statement, "return c2;", is a return statement with an expression, c2. A compile-time error occurs if the type of the expression is not assignable to the declared result type of the method. The declared result type of method m2 is byte. The return statement attempts to return the value of the char type variable c2. If a char value is a compile-time constant, and if the value falls within the range of type byte, then the char value is assignable to type byte. In method m2, variable c2 is not a compile-time constant, because the value of c2 is not known at compile time. Instead, the value of c2 is assigned at run time to the value of the argument. Since the char type variable c2 is not a compile-time constant, the value of variable c2 is not assignable to the return type of method m2 without an explicit cast. While the declaration of method m2 produces a compile-time error, the declaration of method m1 does not. The local variable c1 is declared final and the value is set at compile time; so c1 is a compile-time constant. The value \u0001 falls within the range of type byte; so the value of the compile-time constant c1 is assignable to the return type of method m1 without an explicit cast.  
2
Compile-time error at 2. 
There is a compile-time error at 2. The short type variable s2 is not a compile-time constant, so it can not be assigned to type byte without an explicit cast. The method parameter s2 is declared final, so the value of s2 can not be changed within method m2. The value of method parameter s2 is set at run time to the value of the argument that is provided when m2 is invoked at line 3. For that reason, the method parameter s2 is not a compile-time constant. In method m2, the statement, "return s2;", is a return statement with an expression, s2. A compile-time error occurs if the type of the expression is not assignable to the declared result type of the method. The declared result type of method m2 is byte. The return statement attempts to return the value of the short type variable s2. If a short value is a compile-time constant, and if the value falls within the range of type byte, then the short value is assignable to type byte without an explicit cast. In method m2, variable s2 is not a compile-time constant, because the value of s2 is not known at compile time. Instead, the value of s2 is assigned at run time to the value of the argument. Since the short type variable s2 is not a compile-time constant, the value of variable s2 is not assignable to the return type of method m2 without an explicit cast. While the declaration of method m2 produces a compile-time error, the declaration of method m1 does not. The local variable s1 is declared final and the value is set at compile time; so s1 is a compile-time constant. The value 2 falls within the range of type byte; so the value of the compile-time constant s1 is assignable to the return type of method m1 without an explicit cast.  
3
Compile-time error at 3. 
There is a compile-time error at line 3. The long type variable, l, can not be assigned to type int without an explicit cast. The statement, "return l;", is a return statement with an expression, l. A compile-time error occurs if the type of the expression is not assignable to the declared result type of the method. The declared result type of the method, m3, is int. The type of the variable, l, is long, so an explicit cast is needed to perform the narrowing primitive conversion, "return (int)l;". The declarations of methods m1 and m2 do not generate compile-time errors, because the types of the expressions contained in the return statements are assignable to type int. Widening conversions from types byte, char, or short to type int do not require an explicit cast.  
4
Compile-time error at 2. 
There is a compile-time error at 2, because a narrowing primitive conversion from type float to type int requires an explicit cast. There is no compile-time error at 1, because widening primitive conversions from types byte, char, or short to type int do not require an explicit cast.  
5
d  e  f 
Compile-time error at 2.  Compile-time error at 3.  Compile-time error at 4. 
At line 2, the statement, "return i;", contains the expression, i. The enclosing method, m2, is declared void. The return statement generates a compile-time error, because it contains an expression. At line 3, the statement, "return;", does not contain an expression. The enclosing method, m3, is declared with the result type, int. The return statement generates a compile-time error, because it does not contain an expression that produces a value that is assignable to the declared result type.  




Question Refernce converstion


Question 1

class A {
  public static void main(String[] args) {
    char a = 'a', b = 'b'; // 'a' = 97, 'b' = 98
    System.out.print(a + b + "" + a + b);
}}
What is the result of attempting to compile and run the program?
a.   Prints: 390
b.  Prints: 195195
c.   Prints: 195ab
d.  Prints: ab195
e.   Prints: abab
f.   Run-time error
g.  Compile-time error
h.  None of the above

Question 2

interface I1 {}  interface I2 {}
class Base implements I1 {}
class Sub extends Base implements I2 {}
class Red {
  public static void main(String args[]) {
    Sub s1 = new Sub();
    I2 i2 = s1;          // 1
    I1 i1 = s1;          // 2
    Base base = s1;      // 3
    Sub s2 = (Sub)base;  // 4
}}
A compile-time error is generated at which line?
a.   1
b.  2
c.   3
d.  4
e.   None of the above

Question 3

interface I1 {}  interface I2 {}
class Base implements I1 {}
class Sub extends Base implements I2 {}
class Orange {
  public static void main(String args[]) {
    Base base = new Base();
    I1 i1 = base;           // 1
    Sub sub = (Sub)base;    // 2
}}
What is the result of attempting to compile and run the program?
a.   Compile-time error at line 1
b.  Run-time error at line 1
c.   Compile-time error at line 2
d.  Run-time error at line 2
e.   None of the above

Question 4

interface I1 {}  interface I2 {}
class Base implements I1 {}
class Sub extends Base implements I2 {}
class Yellow {
  public static void main(String args[]) {
    Base base = new Sub();  // 1
    I1 i1 = base;           // 2
    Sub sub = (Sub)base;    // 3
    I2 i2 = (Sub)base;      // 4
}}
A compile-time error is generated at which line?
a.   1
b.  2
c.   3
d.  4
e.   None of the above

Question 5

interface I1 {}  interface I2 {}
class Base implements I1 {}
class Sub extends Base implements I2 {}
class Gray {
  public static void main(String []args) {
  Base[] base = {new Base()};  // 1
  Sub sub[] = {new Sub()};     // 2
  Object obj = sub;            // 3
  base = obj;                  // 4
}}
A compile-time error is generated at which line?
a.   1
b.  2
c.   3
d.  4
e.   None of the above

Question 6

interface I1 {}  interface I2 {}
class Base implements I1 {}
class Sub extends Base implements I2 {}
class Silver {
  public static void main(String []args) {
    Base[] base = {new Base()};
    Sub sub[] = new Sub[1];    // 1
    Object obj = base;         // 2
    sub = (Sub[])obj;          // 3
    I1 []i1 = (I1[])obj;       // 4
}}
What is the result of attempting to compile and run the program?
a.   Compile-time error at line 1
b.  Run-time error at line 1
c.   Compile-time error at line 2
d.  Run-time error at line 2
e.   Compile-time error at line 3
f.   Run-time error at line 3
g.  Compile-time error at line 4
h.  Run-time error at line 4
i.   None of the above



Answers


No.
Answer
Remark
1
Prints: 195ab 
Both operands of the first addition operator are promoted from type char to int, and are evaluated as integral numeric values. The right hand operand of the second addition operator is of type String, so the result of the first addition operator is converted to type String, and is concatenated with the right hand operand. As evaluation of the expression continues from left to right, the remaining operands are also converted to type String.  
2
None of the above 
Line 4 does not generate a compile-time error. The reference named base actually refers to an instance of type Sub, so the reference may be cast to type Sub.  
3
Run-time error at line 2 
The compiler accepts the explicit cast at line 2, but an error is generated at run-time. Type Base is the super class of type Sub, so an instance of type Base can not be converted to the type of the subclass, Sub.  
4
None of the above 
Although the reference named base is of type Base, the instance that it refers to is of type Sub, and the reference can be cast to type Sub. Since instances of type Sub implement both interfaces, I1 and I2, the Sub type instances can be assigned to references of type I1 and I2 without an explicit cast.  
5
The Object referenced by obj is of type Sub[], and the reference, base, is of type Base[]. The assignment expression, base = obj requires an explicit cast to type Base[] as follows: base = (Base[])obj.  
6
Run-time error at line 3 
Base is the superclass of type Sub, so a reference to an actual instance of type Base can not be cast to type Sub. Therefore, a reference to an array instance of type Base[] can not be cast to type Sub[]. The type of the reference, obj, is Object. Type Sub[] is a subclass of Object. The compiler accepts the cast, because the actual instance referenced at run-time might be of type Sub[]. In this case, the actual type of the instance referenced by obj at run-time is found to be type Base[], so the result is a run-time error.  


Question primitive conversion


Question 1

class Black {
  public static void main(String[] args) {
    short s1 = 1;        //1
    char c1 = 1;         //2
    byte b1 = s1;        //3
    byte b2 = c1;        //4
    final short s2 = 1;  //5
    final char c2 = 1;   //6
    byte b3 = s2;        //7
    byte b4 = c2;        //8
}}
Compile-time errors are generated at which lines?
a.   1
b.  2
c.   3
d.  4
e.   5
f.   6
g.  7
h.  8

Question 2

class Magenta {
  static byte a = (byte)127, b = (byte)128, c = (byte)255, d = (byte)256;
  public static void main(String args[]) {
    System.out.print(a + " " + b + " " + c + " " + d);
}}
What is the result of attempting to compile and run the program?
a.   Prints: 127 128 255 256
b.  Prints: 127 128 255 0
c.   Prints: 127 -1 -127 0
d.  Prints: 127 -128 -1 0
e.   Run-time error
f.   Compile-time error
g.  None of the above

Question 3

class Primitives {
  static void printFloat(float f) {System.out.println(f);}
  static void printDouble(double d) {System.out.println(d);}
  public static void main(String[] args) {
    byte b = 1;     // 1
    short s = b;    // 2
    char c = s;     // 3
    int i = c;      // 4
    long l = i;     // 5
    float f = l;    // 6
    printFloat(i);  // 7
    printFloat(l);  // 8
    printDouble(l); // 9
}}
A compile-time error is generated at which line?
a.   1
b.  2
c.   3
d.  4
e.   5
f.   6
g.  7
h.  8
i.   9
j.   None of the above

Question 4

class Maroon {
  public static void main (String[] args) {
    int a = 1;    // 1
    short b = 1;  // 2
    long c = 1;   // 3
    a = c + a;    // 4
    c = b + a;    // 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

Question 5

class Teal {
  public static void main (String[] args) {
    byte b = 1;    // 1
    long l = 1000; // 2
    b += l;        // 3
}}
A compile-time error is generated at which line?
a.   1
b.  2
c.   3
d.  None of the above

Question 6

class Sienna {
  static double a; static float b; static int c; static char d;
  public static void main(String[] args) {
    a = b = c = d = 'a';
    System.out.println(a+b+c+d == 4 * 'a');
}}
What is the result of attempting to compile and run the program?
a.   Prints: true
b.  Prints: false
c.   Compile-time error
d.  Run-time error
e.   None of the above




Question 6

class Sienna {
  static double a; static float b; static int c; static char d;
  public static void main(String[] args) {
    a = b = c = d = 'a';
    System.out.println(a+b+c+d == 4 * 'a');
}}
What is the result of attempting to compile and run the program?
a.   Prints: true
b.  Prints: false
c.   Compile-time error
d.  Run-time error
e.   None of the above


Question 7

class UltraViolet {
  public static void main (String[] args) {
    char a = '\u002a', b = '\u0024'; // a = Asterisk *; b = Dollar Sign $
    System.out.print(a + b);           // 1
    System.out.print(" ABC" + a + b);  // 2
}}
What is the result of attempting to compile and run the program?
a.   Prints: 78 ABC*$
b.  Prints: *$ ABC*$
c.   Prints: 78 ABC78
d.  Compile-time error
e.   Run-time error
f.   None of the above









Answers


No.
Answer
Remark
1
c  d 
3  4 
This question demonstrates a variety of assignment conversions. The compiler will implicitly do a narrowing conversion for an assignment statement if the right hand operand is a compile time constant of type byte, short, char, or int and the value falls within the range of the variable on the left and if the variable on the left is of type byte, short, or char. In this case, variables s1 and c1 are not compile time constants so the compiler will not do an implicit narrowing conversion. However, variables s2 and c2 are compile time constants that fall within the range of the left hand operand. For more information, please see JLS section 5.2.  
2
Prints: 127 -128 -1 0 
Bytes are stored as 8 bit two's complement signed integers. When an int primitive is cast to a byte, the three most significant bytes are discarded and only the least significant byte remains. The most significant bit of the remaining byte becomes the new sign bit. byte a = (byte)127; // 01111111. byte b = (byte)128; // 10000000. byte c = (byte)255; // 11111111. byte d = (byte)256; // 00000000.  
3
Short is signed and char is not signed so an explicit cast is necessary when a short is assigned to a char and vice versa.  
4
The assignment expression, a = c + a, requires an explicit cast to type int. If one of the two operands of a numeric expression is of type long and if the other operand is of type int, short, char or byte; then it will be promoted to type long, and the result of the expression will be of type long. (Note: The rule does not apply to the shift operator.) The type long result can not be assigned to a variable of type int without an explicit cast.  
5
None of the above 
The compound assignment operators include an implicit cast to the type of the left hand operand. The expression at line 3, b += l, does not require an explicit cast to convert the right hand operand from type long to type byte.  
6
Prints: true 
The literal, 'a', is promoted to type int; and is then multiplied by the value of the left operand, 4. If one of the two operands of a numeric expression is of type int, then the other operand will be promoted to type int if it is of type short, char or byte.  
7
Prints: 78 ABC*$ 
When char variables a and b are converted to String types they are printed as *$. When not converted to String types they are promoted to type int, and are printed as the numeric sum of 0x2a and 0x24. At line 1, the expression, a + b, can be evaluated as follows: 0x2a + 0x24 = 42 + 36 = 78. At line 2, the first operand of the expression, " ABC" + a + b, is of type String. Since one operand of the first addition operator is of type String the other operand must be converted to type String: (" ABC" + "*") + b = " ABC*" + b. The process is repeated for the second addition operation: " ABC*" + b = " ABC*" + "$" = " ABC*$"  






Strings Concepts


Question 61

class MCZ27 {
  public static void main (String[] args) {
    char a = '\f';  // 1
    char b = '\n';  // 2
    char c = '\r';  // 3
    char d = '\l';  // 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

Question 62

class MCZ28 {
  public static void main (String[] args) {
    char a = '\b';  // 1
    char b = '\d';  // 2
    char c = '\f';  // 3
    char d = '\t';  // 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

Question 63

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

Question 64

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

Question 65

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

Question 66

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

Question 67

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

Question 68

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

Question 69

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

Question 70

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

Question 71

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

Question 72

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

Question 73

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

Question 74

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

Question 75

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

Question 76

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

Question 77

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


Answers


61
The escape sequences are as follows: '\b' (backspace), '\f' (formfeed), '\n' (newline), '\r' (carriage return), '\t' (horizontal tab), '\\' (backslash), '\"' (double quote), '\'' (single quote). Yes, you must memorize the escape sequences! Just remember "big farms need red tractors".  
62
The escape sequences are as follows: '\b' (backspace), '\f' (formfeed), '\n' (newline), '\r' (carriage return), '\t' (horizontal tab), '\\' (backslash), '\"' (double quote), '\'' (single quote). Yes, you must memorize the escape sequences! Just remember "big farms need red tractors".  
63
The escape sequences are as follows: '\b' (backspace), '\f' (formfeed), '\n' (newline), '\r' (carriage return), '\t' (horizontal tab), '\\' (backslash), '\"' (double quote), '\'' (single quote). Yes, you must memorize the escape sequences! Just remember "big farms need red tractors".  
64
The escape sequences are as follows: '\b' (backspace), '\f' (formfeed), '\n' (newline), '\r' (carriage return), '\t' (horizontal tab), '\\' (backslash), '\"' (double quote), '\'' (single quote). Yes, you must memorize the escape sequences! Just remember "big farms need red tractors".  
65
The escape sequences are as follows: '\b' (backspace), '\f' (formfeed), '\n' (newline), '\r' (carriage return), '\t' (horizontal tab), '\\' (backslash), '\"' (double quote), '\'' (single quote). Yes, you must memorize the escape sequences! Just remember "big farms need red tractors".  
66
Prints: null 
The System.out.print method prints the word null if the argument is a String reference that is null.  
67
None of the above 
Floating-point literals are covered in section 3.10.2 of the JLS. A 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.  
68
The literal 1.0 is a double and can not be used to initialize a float without an explicit cast.  
69
c  d 
3  4 
The compiler interprets \u000a as a line terminator. The escape sequence \n should be used instead. Similarly, \u000d is interpreted as a line terminator. The escape sequence \r should be used instead.  
70
None of the above 
All of the declarations are legal. String b is a single quote followed by the letter A followed by another single quote. String c is the letter A. String d is the Unicode character that is represented by the hexadecimal value D7AF. String literals are covered in section 3.10.5 of the JLS.  
71
None of the above 
String literals are declared using double quotes, but all of the declarations here use single quotes.  
72
None of the above 
Unicode char literals are declared using single quotes, but none of the declarations here use single quotes. The declaration of char b, is also problematic, because it contains more than one char.  
73
None of the above 
All of the declarations are legal. The first three ( 061, '\61', '\061' ) are declared in octal format. The fourth (0x0031) is declared as a hexadecimal literal. The fifth ('\u0031') is a Unicode escape sequence.  
74
d  e 
4  5 
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 in a compile-time error. Java does not accept the hexadecimal escape sequences of the C programming language. However, Java does accept Unicode escapes (JLS 3.3).  
75
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.  
76
char d = -1; 
The assignment of -1 to char d generates a compile-time error, because the primitive char type is unsigned. A negative int can not 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.  
77
boolean b1 = true; 
There are two primitive boolean values: true and false. Both must be written with lower case letters. Although the C programming language accepts zero as a boolean value, the Java programming language does not.  


Static & Instance variables


Question 46

class GFM12 {
  static int x;                             // 1
  public static void main(String[] args) {  // 2
    int y;                                  // 3
    System.out.println("x="+x);             // 4
    System.out.println("y="+y);             // 5
}}
What is the result of attempting to compile and run the program?
a.   Compile-time error at line 1.
b.  Compile-time error at line 2.
c.   Compile-time error at line 3.
d.  Compile-time error at line 4.
e.   Compile-time error at line 5.
f.   Run-time error
g.  None of the above

Question 47

class GFM13 {
  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+c+d+e+s);
}}
What is the result of attempting to compile and run the program?
a.   Prints: 00000null
b.  Prints: 00000
c.   Prints: 0null
d.  Prints: 0
e.   Prints: null
f.   Compile-time error
g.  Run-time error
h.  None of the above

Question 48

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

Question 49

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

Question 50

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

Question 51

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


Question 52

class JJF1 {
  public static void main (String args[]) {
    System.out.print(Byte.MIN_VALUE+",");
    System.out.print(Byte.MAX_VALUE);
}}
What is the result of attempting to compile and run the program?
a.   Prints: 0,255
b.  Prints: 0,256
c.   Prints: -127,128
d.  Prints: -128,127
e.   Compile-time error
f.   Run-time error
g.  None of the above

Question 53

class JJF2 {
  public static void main (String args[]) {
    System.out.print(Short.MIN_VALUE+",");
    System.out.print(Short.MAX_VALUE);
}}
What is the result of attempting to compile and run the program?
a.   Prints: -32767,32768
b.  Prints: -32768,32767
c.   Prints: 0,65535
d.  Prints: 0,65536
e.   Compile-time error
f.   Run-time error
g.  None of the above

Question 54

class JJF3 {
  public static void main(String args[]) {
    System.out.print(Integer.toBinaryString(Byte.MAX_VALUE)+",");
    System.out.print(Integer.toOctalString(Byte.MAX_VALUE)+",");
    System.out.print(Integer.toString(Byte.MAX_VALUE)+",");
    System.out.print(Integer.toHexString(Byte.MAX_VALUE));
}}
What is the result of attempting to compile and run the program?
a.   Prints: 1111111,177,127,7f
b.  Prints: 11111111,377,256,ff
c.   Compile-time error
d.  Run-time error
e.   None of the above

Question 55

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

Question 56

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

Question 57

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

Question 58

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

Question 59

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


Question 60

class MCZ11 {
  public static void main (String[] args) {
    char a = '\c';  // 1
    char b = '\r';  // 2
    char c = '\"';  // 3
    char d = '\b';  // 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



Answers


46
Compile-time error at line 5. 
The local variable y has not been initialized so the attempt to access the variable results in a compile-time error.  
47
Prints: 0null 
The numeric sum of variables a, b, c, d and e is zero. The zero is converted to a String and concatenated with s.  
48
Prints: 0,0,0,0,0,null 
The default value of type char is the null character. When it is cast to an int the value is interpreted as zero.  
49
Prints: 0,0.0,0.0,false,null 
Generally speaking, numeric type variables are initialized to zero. Primitive boolean variables are initialized to false. Reference type variables are initialized to null.  
50
Compile-time 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.  
51
Compile-time error at line 4. 
Anytime a field is accessed from within a static context it is very important to verify that the field is also static. If the field is instead an instance variable then the result is a Compile-time error.  
52
Prints: -128,127 
A byte is an 8 bit signed value; so the minimum byte value is -(27) and the maximum value is (27 - 1).  
53
Prints: -32768,32767 
A short is a 16 bit signed value; so the minimum short value is -(215) and the maximum value is (215 - 1).  
54
Prints: 1111111,177,127,7f 
A byte is an 8 bit signed value. The left most bit is the sign bit. The sign bit is set to zero for positive numbers and is set to one for negative numbers. The most positive byte value is represented as a sign bit that is set to zero and all of the other bits set to one. The Integer.toBinaryString method does not print leading zeros; so only seven bits are printed. An eight bit binary value is represented as three octal digits. The first of the three digits represents the left most two bits of the binary value. In this case, the left most two bits are zero and one. The second octal digit represents the next three bits of the binary value. The last of the octal digits represents the right most three bits of binary value. Note that the Integer.toOctalString method does not print a leading zero as is required for an octal literal value. An eight bit binary value is represented as two hexadecimal digits. The left hex digit represents the left most four bits of the binary value. The right hex digit represents the right most four bits of the binary value.  
55
Prints: 7f,ffff,7fff 
A byte is an 8 bit signed value. A char is a 16 bit unsigned value. A 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.  
56
Prints: 80000000,7fffffff 
An int is a 32 bit signed value. The left most bit is the sign bit. The sign bit is zero for positive numbers and one for negative numbers.  
57
b  d 
'\u0000' to '\uffff'  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 216 - 1.  
58
b  d  f 
0x0000 to 0xffff  0 to 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 216 - 1.  
59
b  c  e 
2  3  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.  
60
The escape sequences are as follows: '\b' (backspace), '\f' (formfeed), '\n' (newline), '\r' (carriage return), '\t' (horizontal tab), '\\' (backslash), '\"' (double quote), '\'' (single quote). Yes, you must memorize the escape sequences! Just remember "big farms need red tractors".