1. public class Test{ static{ int a = 5; } public static void main(String args[]){ new Test().call(); } void call(){ this.a++; System.out.print(this.a); } } What will be the output?
2. public class Test{ public static void main(String args[]){ int x = 10; x = myMethod(x--); System.out.print(x); } static int myMethod(final int x){ return x--; } } What will be the output after the following program is compiled and executed?
3. public class Tester{ static int x = 4; int y = 9; public Tester(){ System.out.print(this.x); // line 1 printVariables(); } public static void printVariables(){ System.out.print(x); // line 2 System.out.print(y); // line 3 } public static void main(String... args) { // line 4 new Tester(); } } What is the result of compiling and running the following code?
4. You have the following code in a file called Test.java: class Base{ public static void main(String[] args){ System.out.println("Hello"); } } public class Test extends Base{} The object is created with new keyword:
5. You have the following code in a file called Test.java: class Base{ public static void main(String[] args){ System.out.println("Hello"); } } public class Test extends Base{} What will happen if you try to compile and run this?
6. public class Test{ public static void main(String args[]){ add(); add(1); add(1, 2); } // insert code here } Choose all the lines which if inserted independently instead of "//insert code here" will allow the following code to compile:
7. static public class Test{ public static void main(String[] args){ char c = 'a'; switch(c){ case 65 : System.out.println("one");break; case 'a': System.out.println("two");break; case 3 : System.out.println("three"); } } } A method within a class is only accessible by classes that are defined within the same package as the class of the method. Which one of the following is used to enforce such restriction?
8. static public class Test{ public static void main(String[] args){ char c = 'a'; switch(c){ case 65 : System.out.println("one");break; case 'a': System.out.println("two");break; case 3 : System.out.println("three"); } } } A package is a collection of:
9. static public class Test{ public static void main(String[] args){ char c = 'a'; switch(c){ case 65 : System.out.println("one");break; case 'a': System.out.println("two");break; case 3 : System.out.println("three"); } } } What will be the output for the below code?
10. public class Tester{ static int x = 4; public Tester(){ System.out.print(this.x); // line 1 Tester(); } public static void Tester(){ // line 2 System.out.print(this.x); // line 3 } public static void main(String... args){ // line 4 new Tester(); } } Choose the correct statement. Restriction on static methods are: I. They can only call other static methods. II. They must only access static data. III. They cannot refer this or super in any way.
11. public class Tester{ static int x = 4; public Tester(){ System.out.print(this.x); // line 1 Tester(); } public static void Tester(){ // line 2 System.out.print(this.x); // line 3 } public static void main(String... args){ // line 4 new Tester(); } } What is the result of compiling and running the following code?
12. class Base{ private Base(){ System.out.print("Base"); } } public class test extends Base{ public test(){ System.out.print("Derived"); } public static void main(String[] args){ new test(); } } Name the keyword that makes a variable belong to a class, rather than being defined for each instance of the class.
13. class Base{ private Base(){ System.out.print("Base"); } } public class test extends Base{ public test(){ System.out.print("Derived"); } public static void main(String[] args){ new test(); } } What is the result of compiling and running the following code?
14. public class A{ static{ System.out.println("static"); } { System.out.println("block"); } public A(){ System.out.println("A"); } public static void main(String[] args){ A a = new A(); } } What is the output for the below code?