Mock Exam
| class | A { | ||||||
| public | A() { | ||||||
| class | B{ | ||||||
| B() | { | ||||||
| System.out.println("In no-arg constructor"); | |||||||
| } | |||||||
| } | |||||||
| new B(); | |||||||
| } | |||||||
| public | A(int a) { | ||||||
| class | B{ | ||||||
| B() | { | ||||||
| System.out.println("In constructor with an int argument"); | |||||||
| } | |||||||
| } | |||||||
| new B(); | |||||||
| } | |||||||
| public | static void | main( | String[] args{ | ||||
| A a1 = new | A(); | ||||||
| A a2 = new | A(10); | ||||||
| } | |||||||
| } | |||||||
| In no-arg constructor |
| In constructor with an int argument |
| System.out.println("-0.0 == 0.0 returns " + (-0.0 == 0.0)); |
| System.out.println("0.0 > -0.0 returns " + (0.0 > -0.0)); |
___________________________________
| public | class | Temp{ |
| public | static void main(String[] args){ | |
| String[] a = null; | ||
| System.out.println("The length of the array is " + a.length): | ||
| } | ||
| } |
Which of the following are valid values that can be assigned to a?
| class | Base { | |
| public | static void amethod(){ | |
| System.out.println("In base amethod"); | ||
| } | ||
| public | void another(){ | |
| System.out.println("In base another"); | ||
| } | ||
| static | int staticInt = 10; | |
| int | instanceVar = 20; | |
| } | ||
| public | class | Child extends Base { |
| public | static void amethod(){ | |
| System.out.println("In child amethod"); | ||
| } | ||
| public | void another() { | |
| System.out.println("In child another"); | ||
| } | ||
| static | int staticInt = 30; | |
| int | instanceVar = 40; | |
| public | static void main(String[] a ){ | |
| Base b = new Child(); | ||
| b.amethod(); | ||
| b.another(); | ||
| System.out.println("Value of staticInt is = " + b.staticInt); | ||
| System.out.println("Value of instanceVar is = " + b.instanceVar); | ||
| } | ||
| } |
In base amethod
In child another
Value of staticInt is 30
Value of instanceVar is 40
In base amethod
In child another
Value of staticInt is 10
Value of instanceVar is 20
In child amethod
In child another
Value of staticInt is 30
Value of instanceVar is 40
| class | A{ | |
| public | static void main(String[] args){ | |
| int i = 1; | ||
| int j = 5; | ||
| System.out.println((i++ * j)); | ||
| System.out.println("i = " + i + " j = "+ j); | ||
| System.out.println((++i * j)); | ||
| System.out.println("i = " + i + " j = " + j); | ||
| } | ||
| } |
| 10 |
| I = 2 j=5 |
| 15 |
| I = 3 j = 5 |
| 5 |
| I = 2 j=5 |
| 15 |
| I = 3 j = 5 |
ANSWERS:-
Remember, that a compound assignment expression of the form E1 op= E2 is always equivalent to E1 = (Type) (E1 op E2), where Type is the type of E1.
The only modifiers that are allowed with interface methods are public and abstract. And these are implicit, so you dont even have to include them.
All three are right. Classes defined within an interface are implicitly public and static and because you cannot have static methods within an iterface, you cannot refer to the non-static methods from the static class methods.
The only operators that can cause an ArithmeticException are the integer division (/) and modulo (%) operators. Remember that float operations do not raise any exception at all. They may result in NaN or Infinities, instead.
-0.0 > 0.0 returns false
Check out the Java Language Specification for more information on Character Literal and Escape Sequences
Static variables and methods as well as instance variables use the Type of the reference variable to determine the correct variable/method to use. On the other hand, instance methods use the Class of the reference variable to determine the correct method to call
Unlike methods, a constructor cannot be abstract, static, final, strictfp, native or synchronized.