Data Types And Variables - Study Mode
[#81] 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 )
Correct Answer
Does not compile as Integer and String are API class names.
[#82] 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 )
}
}
Correct Answer
(C) 22
Explanation
Solution: 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
[#83] 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)
}
}
Correct Answer
(C) Compilation Error
Explanation
Solution: lossy conversion from int to short x = x*5
^ 1 error
[#84] Determine output: public class Test{
int i = 34
public static void main(String args[]){
Test t1 = new Test()
Test t2 = new Test()
t1.i = 65
System.out.print(t1.i)
System.out.print(t2.i)
}
}
Correct Answer
(B) 65 34
Explanation
Solution: Instance variable is unique for their object, that means each copy of memory for variable i will be available for each object. So, changing value of i from one object will not affect the value of i accessed by another object. 1. Two objects of the Test class, t1 and t2 , are created using the new keyword. 2. The default value of the instance variable i is 34 as defined in the class Test . 3. The instance variable i of t1 is then set to 65 using t1.i = 65
4. When we print t1.i , it will output 65 . 5. When we print t2.i , it will output 34 . This is because t2 is a separate instance of the Test class, and its i remains the default value of 34 . Therefore, the correct output will be 6534 .
[#85] The following program: public class Test{
static boolean isOK
public static void main(String args[]){
System.out.print(isOK)
}
}
Correct Answer
(B) Prints false
Explanation
Solution: By default, a boolean variable will contain false.