ExamVeda
Login
Home
51
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._$);
      }
}
Discuss
Answer & Solution
Answer: Option D
Solution:

$7 is valid identifier. Identifiers must start with a letter, a currency character ($), or underscore ( _ ). Identifiers cannot start with a number. You can't use a Java keyword as an identifier. do is a Java keyword.

52
What is the output for the below code?
public class Test{
      public static void main(String[] args){
            int i = 010;
            int j = 07;
            System.out.println(i);
            System.out.println(j);
      }
}
Discuss
Answer & Solution
Answer: Option A
Solution:

By placing a zero in front of the number is an integer in octal form. 010 is in octal form so its value is 8

53
What is the output for the below code?
public class Test{
      public static void main(String[] args){
            byte b = 6;
            b+=8;
            System.out.println(b);
            b = b+7;
            System.out.println(b);
      }
}
Discuss
Answer & Solution
Answer: Option C
Solution:

int or smaller expressions always resulting in an int. So compiler complain about Type mismatch: cannot convert from int to byte for b = b+7; But b += 7; // No problem because +=, -=, *=, and /= will all put in an implicit cast. b += 7 is same as b = (byte)b+7 so compiler not complain.

54
What will be the output for the below code?
public class Test{
      public static void main(String[] args){
            byte i = 128;
            System.out.println(i);
      }
}
Discuss
Answer & Solution
Answer: Option C
Solution:
byte can only hold up to 127. So compiler complain about possible loss of precision.
static byte MAX_VALUE − This is constant holding the maximum value a byte can have, 27-1.
static byte MIN_VALUE − This is constant holding the minimum value a byte can have, -27.
55
What will be the output for the below code?
public class Test{
      int i=8;
      int j=9;
      public static void main(String[] args){
            add();
      }
      public static void add(){
            int k = i+j;
            System.out.println(k);
     }
}
Discuss
Answer & Solution
Answer: Option D
Solution:

i and j are instance variable and attempting to access an instance variable from a static method. So Compilation fails.

56
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);
      }
}
Discuss
Answer & Solution
Answer: Option C
Solution:

Range of byte data in java is -128 to 127. But byte data type in java is cyclic in nature.

57
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);
      }
}
Discuss
Answer & Solution
Answer: Option B
Solution:
Here's an explanation of the code:

1. The class Test is defined with an instance variable a initialized to 10.
2. The method method is defined within the Test class, which takes an integer parameter a.
3. Inside the method method, the local variable a shadows the instance variable a because they have the same name.
4. The value of the local variable a is incremented by 1 using the += operator, making it 4.
5. The value of the local variable a is then incremented again using the pre-increment operator ++, making it 5.
6. The value of the local variable a (which is 5) is printed using System.out.println().
7. In the main method, an instance of the Test class is created.
8. The method method is invoked on the t object with the argument 3.
9. This triggers the execution of the method method, which prints 5 as explained above.

Therefore, the output of the code is 5. Thus, option B is the correct answer.
58
Which class does all the Enums extend?
Discuss
Answer & Solution
Answer: Option C
No explanation is given for this question. Let's Discuss on Board
59
Which of the following is the advantage of BigDecimal over double?
Discuss
Answer & Solution
Answer: Option D
No explanation is given for this question. Let's Discuss on Board
60
What will be the output of the following Java code snippet?
double a = 0.02;
double b = 0.03;
double c = b - a;
System.out.println(c);

BigDecimal _a = new BigDecimal("0.02");
BigDecimal _b = new BigDecimal("0.03");
BigDecimal _c = b.subtract(_a);
System.out.println(_c);
Discuss
Answer & Solution
Answer: Option A
No explanation is given for this question. Let's Discuss on Board