Sail E0 Webinar

MCQs

Total Questions : 45 | Page 3 of 5 pages
Question 21. What is the output for the below code?
public class Test{
int _$;
int $7;
int do;
public static void main(String argv[]){
Test test = new Test();
test.$7=7;
test.do=9;
System.out.println(test.$7);
System.out.println(test.do);
System.out.println(test._$);
}
}
  1.    7 9 0
  2.    7 0 0
  3.    Compile error - $7 is not valid identifier.
  4.    Compile error - do is not valid identifier.
  5.    None of these
 Discuss Question
Answer: Option D. -> Compile error - do is not valid identifier.
Question 22. What is the output for the below code ?
1. public class Test{
2. public static void main(String[] args){
3. int i = 010;
4. int j = 07;
5. System.out.println(i);
6. System.out.println(j);
7. }
8. }
  1.    8 7
  2.    10 7
  3.    Compilation fails with an error at line 3
  4.    Compilation fails with an error at line 5
  5.    None of these
 Discuss Question
Answer: Option A. -> 8 7
Question 23. What will be the output for the below code ?
1. public class Test{
2. public static void main(String[] args){
3. byte i = 128;
4. System.out.println(i);
5. }
6. }
  1.    128
  2.    0
  3.    Compilation fails with an error at line 3
  4.    Compilation fails with an error at line 4
  5.    None of these
 Discuss Question
Answer: Option C. -> Compilation fails with an error at line 3
Question 24. What is the output for the below code ?
1. public class Test{
2. public static void main(String[] args){
3. byte b = 6;
4. b+=8;
5. System.out.println(b);
6. b = b+7;
7. System.out.println(b);
8. }
9. }
  1.    14 21
  2.    14 13
  3.    Compilation fails with an error at line 6
  4.    Compilation fails with an error at line 4
  5.    None of these
 Discuss Question
Answer: Option C. -> Compilation fails with an error at line 6
Question 25. What will be the output for the below code ?
1. public class Test{
2. int i=8;
3. int j=9;
4. public static void main(String[] args){
5. add();
6. }
7. public static void add(){
8. int k = i+j;
9. System.out.println(k);
10. }
11. }
  1.    17
  2.    0
  3.    Compilation fails with an error at line 5
  4.    Compilation fails with an error at line 8
  5.    None of these
 Discuss Question
Answer: Option D. -> Compilation fails with an error at line 8
Question 26. What will be output of following program?
public class Test{
public static void main(String[] args){
byte b=127;
b++;
b++;
System.out.println(b);
}
}
  1.    2
  2.    129
  3.    -127
  4.    Compiler error
  5.    None of these
 Discuss Question
Answer: Option C. -> -127
Question 27. Determine output:
public class Test{
int a = 10;
public void method(int a){
a += 1;
System.out.println(++a);
}
public static void main(String args[]){
Test t = new Test();
t.method(3);
}
}
  1.    4
  2.    5
  3.    12
  4.    11
  5.    None of these
 Discuss Question
Answer: Option B. -> 5
Question 28.


What is the output of this program?


class area {
public static void main(String args[])
{
double r, pi, a;
r = 9.8;
pi = 3.14;
a = pi * r * r;
System.out.println(a);
}
}
  1.    301.5656
  2.    301
  3.    301.56
  4.    301.56560000
 Discuss Question
Answer: Option A. -> 301.5656

None.
output:
$ javac area.java
$ java area
301.5656


Question 29.


What is the output of this program?


class increment {
public static void main(String args[])
{
int g = 3;
System.out.print(++g * 8);
}
}
  1.    25
  2.    24
  3.    32
  4.    33
 Discuss Question
Answer: Option C. -> 32

Operator ++ has more preference than *, thus g becomes 4 and when multiplied by 8 gives 32.
output:
$ javac increment.java
$ java increment
32


Question 30.


What is the output of this program?


class conversion {
public static void main(String args[])
{
double a = 295.04;
int b = 300;
byte c = (byte) a;
byte d = (byte) b;
System.out.println(c + " " + d);
}
}
  1.    38 43
  2.    39 44
  3.    295 300
  4.    295.04 300
 Discuss Question
Answer: Option B. -> 39 44

Type casting a larger variable into a smaller variable results in modulo of larger variable by 

range of smaller variable. b contains 300 which is larger than byte's range i:e -128 to 127 

hence d contains 300 modulo 256 i:e 44.
output:
$ javac conversion.java
$ java conversion
39 44



Latest Videos

Latest Test Papers