Followers

Sunday, 5 August 2012

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".  




No comments:

Post a Comment