Data Types And Variables(Java Program ) Questions and Answers

Question 1. Determine output:
public class Test {
static void test(float x){
System.out.print("float");
}
static void test(double x){
System.out.print("double");
}
public static void main(String[] args){
test(99.9);
}
}
  1.    float
  2.    double
  3.    Compilation Error
  4.    Exception is thrown at runtime
Explanation:-
Answer: Option B. -> double
floating-point numbers are by default of type double.
99.9 is a double not a float.
To print "float" cast 99.9 to (float)

Question 2. The following fraction of code
double STATIC = 2.5 ;
System.out.println( STATIC );
  1.    Prints 2.5
  2.    Rraises an error as STATIC is used as a variable which is a keyword
  3.    Raises an exception
  4.    None of these
Explanation:-
Answer: Option A. -> Prints 2.5

Question 3. What will be output of the following program code?
public class Test{
public static void main(String[] a){
short x = 10;
x = x*5;
System.out.print(x);
}
}
  1.    50
  2.    10
  3.    Compilation Error
  4.    None of these
Explanation:-
Answer: Option C. -> Compilation Error
lossy conversion from int to short
x = x*5;
       ^
1 error

Question 4. What is the output of the following program?
public class Test{
static int x = 10 ;
public static void main(String[] a){
Test test = new Test( ) ;
Test test1 = new Test( ) ;
test.x += 1 ;
System.out.println( test.x + test1.x ) ;
}
}
  1.    20
  2.    21
  3.    22
  4.    Compilation Error
  5.    Throws Exception
Explanation:-
Answer: Option C. -> 22
Static variable have a single copy of memory. That means all the objects will share the same memory location. So, if the object test increase the value of x by 1, then object test1 will access that incremented value of x

Question 5. What would be the output of the following fraction of code ?
int Integer = 34 ;
char String = 'S' ;
System.out.print( Integer ) ;
System.out.print( String ) ;
  1.    Does not compile as Integer and String are API class names.
  2.    Throws exception.
  3.    34
  4.    S
  5.    34 S
Explanation:-
Answer: Option E. -> 34 S

Question 6. 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
Explanation:-
Answer: Option D. -> Compile error - do is not valid identifier.
$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.

Question 7. 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
Explanation:-
Answer: Option A. -> 8 7
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

Question 8. 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
Explanation:-
Answer: Option D. -> Compilation fails with an error at line 8
i and j are instance variable and attempting to access an instance variable from a static method. So Compilation fails.

Question 9. 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
Explanation:-
Answer: Option C. -> Compilation fails with an error at line 6
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.

Question 10. 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
Explanation:-
Answer: Option C. -> Compilation fails with an error at line 3
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.