0% found this document useful (0 votes)
9K views8 pages

Java Test Paper

The document contains a 50 question Java test covering topics like object-oriented programming concepts, inheritance, constructors, access modifiers, arrays, and exceptions. The questions are multiple choice and test understanding of Java syntax, semantics, and behavior.

Uploaded by

Ragini Bajpai
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
0% found this document useful (0 votes)
9K views8 pages

Java Test Paper

The document contains a 50 question Java test covering topics like object-oriented programming concepts, inheritance, constructors, access modifiers, arrays, and exceptions. The questions are multiple choice and test understanding of Java syntax, semantics, and behavior.

Uploaded by

Ragini Bajpai
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1/ 8

JAVA TEST-1 Total 50 marks Objective (30*1=30) (1) What will be the result of compiling and running the

given program? Select one correct answer. class A { void callme() { System.out.println("A"); } int r=10; } class B extends A { public void callme() { System.out.println("B"); } int r=20; } class Q16 { public static void main(String args[]) { A a = new B(); a.callme(); System.out.println(a.r); } } (a) Compile time error. (b) Program compiles correctly and prints "A" and 10 when executed. (c) Program compiles correctly and prints "B" and 20 when executed. (d) Program compiles correctly and prints "B" and 10 when executed. (2) What will happen if you attempt to compile and run the following code? class Base {} class Sub extends Base {} class Sub2 extends Base {} public class CEx { public static void main(String argv[]){ Base b = new Base(); Sub s = (Sub) b; } } (a) Compile and run without error. (b) Compile time Exception. (c) Runtime Exception. (3) Select the order of access modifiers from least restrictive to most restrictive. A. public, private, protected, default B.default,protected,private,public C.public, default,protected,private D.default,public,protected,private E.public,protected, default,private

(4) Given a class named Book, which of these would be valid definitions of constructors for the class? Select all valid answers: (a) Book(Book b) {} (b) Book Book() {} (c) private final Book() {} (d) void Book() {} (e) public static void Book(String args[]) {} (f) abstract Book() {} (5) What will happen when you attempt to compile and run this code? private class Base{} public class Vis{ transient int iVal; public static void main(String elephant[]){ } } (a) Compile time error: Base cannot be private. (b) Compile time error indicating that an integer cannot be transient. (c) Compile time error transient not a data type. (d) Compile time error malformed main method. (6) What will happen when you attempt to compile and run the following code? public class Hope{ public static void main(String argv[]) { Hope h = new Hope(); } protected Hope() { for(int i =0; i <10; i ++){ System.out.println(i); } } } (a) Compilation error: Constructors cannot be declared protected. (b) Run time error: Constructors cannot be declared protected. (c) Compilation and running with output 0 to 10. (d) Compilation and running with output 0 to 9. (7) What will happen when you attempt to compile and run the following code with the command line "hello there"? public class Arg{ String[] MyArg; public static void main(String argv[]){ MyArg=argv; } public void amethod(){ System.out.println(argv[1]); } } (a) Compile time error. (b) Compilation and output of "hello". (c) Compilation and output of "there". (d) None of the above. (8) 1. public class SwitchTest { 2. public static void main (String []args) { 3. System.out.PrintIn(value = +switchIt(4));

4. } 5. public static int switchIt(int x) { 6. int j = 1; 7. switch (x) { 8. case 1: j++; 9. case 2: j++; 10. case 3: j++; 11. case 4: j++; 12. case 5: j++; 13. default:j++; 14. } 15. return j + x; 16. } 17. } What is the output from line 3? A. Value = 3 B. Value = 4 C. Value = 5 D. Value = 6 E. Value = 7 F. Value = 8 (9) Which two create an instance of an array? (Choose Two) A. int[] ia = new int [15]; B. float fa = new float [20]; C. char[] ca = Some String; D. Object oa = new float[20]; E. Int ia [][] = (4, 5, 6) (1, 2, 3) (10) You want a class to have access to members of another class in the same package. Which is the most restrictive access modifier that will accomplish that will accomplish this objective? A. Public B. Private C. Protected D. Transient E. No access modifier is required. (11) Which two statements are true regarding the creation of a default constructor? (Choose Two) A. The default constructor initializes method variables. B. The default constructor invokes the no-parameter constructor of the superclass. C. The default constructor initializes the instance variables declared in the class. D. If a class lacks a no-parameter constructor,, but has other constructors, the compiler creates a default constructor. E. The compiler creates a default constructor only when there are no other constructors for the class. (12) 1. public class X { 2. private static int a; 3. 5. public static void main (String[] args) { 6. modify (a); 7. } 8. 9. public static void modify (int a) { 10. a++;

11. } 12. } What is the result? A. The program runs and prints 0 B. The program runs and prints 1 C. The program runs but aborts with an exception. D. En error possible undefined variable at line 5 causes compilation to fail. F. En error possible undefined variable at line 10 causes compilation to fail. (13) 1. switch (i) { 2. default: 3. System.out.printIn(Hello); 4. } What is the acceptable type for the variable i? A. Byte B. Long C. Float D. Double E. Object F. A and B G. C and D (14) 1. public class X { 2. public static void main (String[]args) { 3. int [] a = new int [1] 4. modify(a); 5. System.out.printIn(a[0]); 6. } 7. 8. public static void modify (int[] a) { 9. a[0] ++; 10. } 11. } What is the result? A. The program runs and prints 0 B. The program runs and prints 1 C. The program runs but aborts with an exception. D. An error possible undefined variable at line 4 causes compilation to fail. E. An error possible undefined variable at line 9 causes compilation to fail. (15) 8. public class X { 9. public static void main (String[] args) { 10. byte b = 127; 11. byte c = 126; 12. byte d = b + c; 13. } 14. } Which statement is true? A. Compilation succeeds and d takes the value 253. B. Line 5 contains an error that prevents compilation. C. Line 5 throws an exception indicating Out of range D. Line 3 and 4 contain error that prevent compilation. E. The compilation succeeds and d takes the value of 1. (16) 1. public class Foo { 2. public static void main (String []args) { 3. int i = 1; 4. int j = i++;

5. if ((i>++j) && (i++ ==j)) { 6. i +=j; 7. } 8. } 9. } What is the final value of i? A. 1 B. 2 C. 3 D. 4 E. 5 (17) 1. public class Test { 2. public static void main (String[]args) { 3. String foo = args[1]; 4. String bar = args[2]; 5. String baz = args[3]; 6. System.out.printIn(baz = + baz); 7. } 8. } And the output: Baz = 2 Which command line invocation will produce the output? A. Java Test 2222 B. Java Test 1 2 3 4 C. Java Test 4 2 4 2 D. Java Test 4 3 2 1 (18) 1. public class ArrayTest { 2. public static void main (String[]args) { 3. float f1[], f2[]; 4. f1 = new float [10]; 5. f2 = f1; 6. System.out.printIn (f2[0]= + f2[0]); 7. } 8. } What is the result? A. It prints f2[0] = 0.0 B. It prints f2[0] = NaN C. An error at line 5 causes compile to fail. D. An error at line 6 causes compile to fail. E. An error at line 6 causes an exception at runtime. (19) 9. class BaseClass{ 10. private float x= 1.0f; 11. protected void setVar (float f) {x = f;} 12. } 13. class SubClass extends BaseClass { 14. private float x = 2.0f; 15. //insert code here 16. } Which two are valid examples of method overriding? (Choose Two) A. Void setVar(float f) {x = f;} B. Public void setVar(int f) {x = f;} C. Public void setVar(float f) {x = f;} D. Public double setVar(float f) {x = f;} E. Public final void setVar(float f) {x = f;} F. Protected float setVar() {x=3.0f; return 3.0f; }

(20) 1. public class IfTest ( 2. public static void main(string[]args) { 3. int x = 3; 4. int y = 1; 5. if (x = y) 6. system.out.printIn(Not equal); 7. else 8. system.out.printIn(Equal); 9. } 10. ) What is the result? A. The output is Equal B. The output in Not Equal C. An error at line 5 causes compilation to fall. D. The program executes but does not print a message. (21) 1. interface foo { 2. int k = 0; 3. } 4. 5. public class test implements Foo ( 6. public static void main(String args[]) ( 7. int i; 8. Test test = new test (); 9. i= test.k; 10.i= Test.k; 11.i= Foo.k; 12.) 13.) 14. What is the result? A. Compilation succeeds. B. An error at line 2 causes compilation to fail. C. An error at line 9 causes compilation to fail. D. An error at line 10 causes compilation to fail. E. An error at line 11 causes compilation to fail. (22) Which three are valid declarations of a float? (Choose Three) A. Float foo = -1; B. Float foo = 1.0; C. Float foo = 42e1; D. Float foo = 2.02f; E. Float foo = 3.03d; F. Float foo = 0x0123; (23) 11. public void test(int x) { 12. int odd = x%2; 13. if (odd) { 14. System.out.println(odd); 15. } else { 16. System.out.println(even); 17. } 18. } Which statement is true? A. Compilation fails. B. odd will always be output. C. even will always be output. D. odd will be output for odd values of x, and even for even values. E. even will be output for add values of x, and odd for even values.

(24) What is the output of following block of program ? boolean var = false; if(var = true) { System.out.println(TRUE); } else { System.out.println(FALSE); } (a) TRUE (b) FALSE (c) Compilation Error (d) Run-time Error (25) Is an empty file a valid source file? (a) True. (b) False. (26) Which of the following lines are valid declarations? (a) char a = \u0061; (b) char \u0061 = a; (c) ch\u0061r a = a; (d) char a = (char) 65; (27) What will be the result of compiling and running the given program? Select one correct answer. 1 class Q1 2{ 3 public static void main(String arg[]) 4{ 5 int a[]={2,2}; 6 int b=1; 7 a[b]=b=0; 8 System.out.println(a[0]); 9 System.out.println(a[1]); 10 } 11 } (a) Compile time error at the line no. 5. (b) Run time error at the line no. 5. (c) Program compiles correctly and print 2,0 when executed. (d) Program compiles correctly and print 0,2 when executed. (28) What will happen if you compile/run the following code? 1.public class Q11 2.{ 3. static String str1=main method with String[] args; 4. static String str2=main method with int[] args; 5. public static void main(String[] args) 6. { 7. System.out.println(str1); 8. } 9. public static void main(int[] args) 10. { 11. System.out.println(str2); 12. } 13.} A) Duplicate method main(), compilation error at line 5. B) Duplicate method main(), compilation error at line 9.

C) prints main method with String[] args. D) prints main method with int[] args. (29) What will happen when you attempt to compile and run the following code public class Hope { public static void main(String[] args) { Hope h=new Hope(); } protected Hope() { for(int i=0;i<10;i++) { System.out.println(i); } } } A. Compilation error: Constructors cant be declared protected B. Run time error: Constructors cant be declared protected C. Compilation and running with output 0 to 10. D. Compilation and running with output 0 to 9. 30 . Given the class public class Args { public static void main(String[] args) { System.out.println(args[0] + " " + args[args.length-1]); } } what would be the result of executing the following on the command line? java Args In politics stupidity is not a handicap a. The program will throw ArrayIndexOutOfBoundsException b. The program will print "java handicap". c. The program will print "Args handicap". d. The program will print "In handicap". Subjective (4*5=20) 31. a. Create a base class, Telephone, and derive a class ElectronicPhone from it. In Telephone, create a protected string member phonetype, and a public method Ring( ) that outputs a text message like this: "Ringing the <phonetype>." In ElectronicPhone, the constructor should set the phonetype to "Digital." In the Run( ) method, call Ring( ) on the ElectronicPhone to test the inheritance. Extend the above to illustrate a polymorphic method. Have the derived class override the Ring( ) method to display a different message. What is the difference between an instance variable and a static variable? What are the access modifiers?

b. 32. a. b.

33. Explain a. static, abstract and final keywords b. Benefits of Object oriented programming 34. a. Difference between overloading and overriding b. Write short notes on JDK, JVM and JRE

You might also like