Java Questions and Answers Part (1) ================== Control - TopicsExpress



          

Java Questions and Answers Part (1) ================== Control Statements 1. Which of these selection statements test only for equality? a) if b) switch c) Both a & b d) None of the mentioned Answer Answer: b Explanation: switch statements checks for equality between the controlling variable and its constant cases. 2. Which of these are selection statements in Java? a) if() b) for() c) continue d) break Answer:a Explanation: continue and break are jump statements, and for is an looping statement. 3. Which of the following loops will execute the body of loop even when condition controlling the loop is initially false? a) do-while b) while c) for d) None of the mentioned Answer: a Explanation: None. 4. Which of these jump statements can skip processing remainder of code in its body for a particular iteration? a) break b) return c) exit d) continue Answer: d Explanation: None. 5. Which of these statement is correct? a) switch statement is more efficient than a set of nested ifs. b) two case constants in the same switch can have identical values. c) switch statement can only test for equality, whereas if statement can evaluate any type of boolean expression. d) it is possible to create a nested switch statements. Answer: b Explanation: No two case constants in the same switch can have identical values. 6. What is the output of this program? 1. class selection_statements { 2. public static void main(String args[]) 3. { 4. int var1 = 5; 5. int var2 = 6; 6. if ((var2 = 1) == var1) 7. System.out.print(var2); 8. else 9. System.out.print(++var2); 10. } 11. } a) 1 b) 2 c) 3 d) 4 Answer:4 Explanation: None. output: $ javac selection_statements.java $ java selection_statements 2 7. What is the output of this program? 1. class comma_operator { 2. public static void main(String args[]) 3. { 4. int sum = 0; 5. for (int i = 0, j = 0; i < 5 & j < 5; ++i, j = i + 1) 6. sum += i; 7. System.out.println(sum); 8. } 9. } a) 5 b) 6 c) 14 d) compilation error Answer: b Explanation: Using comma operator , we can include more than one statement in the initialization and iteration portion of the for loop. Therefore both ++i and j = i + 1 is executed i gets the value – 0,1,2,3,4 & j gets the values -0,1,2,3,4,5. output: $ javac comma_operator.java $ java comma_operator 6 8. What is the output of this program? 1. class jump_statments { 2. public static void main(String args[]) 3. { 4. int x = 2; 5. int y = 0; 6. for ( ; y < 10; ++y) { 7. if (y % x == 0) 8. continue; 9. else if (y == 8) 10. break; 11. else 12. System.out.print(y + " "); 13. } 14. } 15. } a) 1 3 5 7 b) 2 4 6 8 c) 1 3 5 7 9 d) 1 2 3 4 5 6 7 8 9 Answer:c Explanation: Whenever y is divisible by x remainder body of loop is skipped by continue statement, therefore if condition y == 8 is never true as when y is 8, remainder body of loop is skipped by continue statements of first if. Control comes to print statement only in cases when y is odd. output: $ javac jump_statments.java $ java jump_statments 1 3 5 7 9 9. What is the output of this program? 1. class Output { 2. public static void main(String args[]) 3. { 4. int x, y = 1; 5. x = 10; 6. if (x != 10 && x / 0 == 0) 7. System.out.println(y); 8. else 9. System.out.println(++y); 10. } 11. } a) 1 b) 2 c) Runtime error owing to division by zero in if condition. d) Unpredictable behavior of program. Answer: b Explanation: Operator short circuit and, &&, skips evaluating right hand operand if left hand operand is false thus division by zero in if condition does not give an error. output: $ javac Output.java $ java Output 2 10. What is the output of this program? 1. class Output { 2. public static void main(String args[]) 3. { 4. int a = 5; 5. int b = 10; 6. first: { 7. second: { 8. third: { 9. if (a == b >> 1) 10. break second; 11. } 12. System.out.println(a); 13. } 14. System.out.println(b); 15. } 16. } 17. } a) 5 10 b) 10 5 c) 5 d) 10 Answer: d Explanation: b >> 1 in if returns 5 which is equal to a i:e 5, therefore body of if is executed and block second is exited. Control goes to end of the block second executing the last print statement, printing 10. output: $ javac Output.java $ java Output 10 class fundamentals & object declaration in Java Programming Language =================================. 1. What is the stored in the object obj in following lines of code? box obj; a) Memory address of allocated memory of object. b) NULL c) Any arbitrary pointer d) Garbage Answer: b Explanation: Memory is allocated to an object using new operator. box obj; just declares a reference to object, no memory is allocated to it hence it points to NULL. 2. Which of these keywords is used to make a class? a) class b) struct c) int d) None of the mentioned Answer:a Explanation: None. 3. Which of the following is a valid declaration of an object of class Box? a) Box obj = new Box(); b) Box obj = new Box; c) obj = new Box(); d) new Box obj; Answer: a Explanation: None. 4. Which of these operators is used to allocate memory for an object? a) malloc b) alloc c) new d) give Answer: c Explanation: Operator new dynamically allocates memory for an object and returns a reference to it. This reference is address in memory of the object allocated by new. 5. Which of these statement is incorrect? a) Every class must contain a main() method. b) Applets do not require a main() method at all. c) There can be only one main() method in a program. d) main() method must be made public. Answer: a Explanation: Every class does not need to have a main() method, there can be only one main() method which is made public. 6. What is the output of this program? 1. class main_class { 2. public static void main(String args[]) 3. { 4. int x = 9; 5. if (x == 9) { 6. int x = 8; 7. System.out.println(x); 8. } 9. } 10. } a) 9 b) 8 c) Compilation error d) Runtime error Answer: c Explanation: Two variables with the same name can’t be created in a class. output: $ javac main_class.java Exception in thread “main” java.lang.Error: Unresolved compilation problem: Duplicate local variable x 7. Which of the following statements is correct? a) Public method is accessible to all other classes in the hierarchy b) Public method is accessible only to subclasses of its parent class c) Public method can only be called by object of its class. d) Public method can be accessed by calling object of the public class. Answer: a Explanation: None. 8. What is the output of this program? 1. class box { 2. int width; 3. int height; 4. int length; 5. } 6. class mainclass { 7. public static void main(String args[]) 8. { 9. box obj = new box(); 10. obj.widht = 10; 11. obj.height = 2; 12. obj.length = 10; 13. int y = obj.widht * obj.height * obj.length; 14. System.out.print(y); 15. } 16. } a) 12 b) 200 c) 400 d) 100 Answer: b Explanation: None. output: $ javac mainclass.java $ java mainclass 200 9. What is the output of this program? 1. class box { 2. int width; 3. int height; 4. int length; 5. } 6. class mainclass { 7. public static void main(String args[]) 8. { 9. box obj1 = new box(); 10. box obj2 = new box(); 11. obj1.height = 1; 12. obj1.length = 2; 13. obj1.width = 1; 14. obj2 = obj1; 15. System.out.println(obj2.height); 16. } 17. } a) 1 b) 2 c) Runtime error d) Garbage value Answer: a Explanation: When we assign an object to another object of same type, all the elements of right side object gets copied to object on left side of equal to, =, operator. output: $ javac mainclass.java $ java mainclass 2 10. What is the output of this program? 1. class box { 2. int width; 3. int height; 4. int length; 5. } 6. class mainclass { 7. public static void main(String args[]) 8. { 9. box obj = new box(); 10. System.out.println(obj); 11. } 12. } a) 0 b) 1 c) Runtime error d) Garbage value Answer: d Explanation: Object obj of box class contains reference to the memory which was given to its class instances. Printing obj will print the address of the memory. output: $ javac mainclass.java $ java mainclass box@130671e Introduction To Methods ============= 1. What is the return type of a method that does not returns any value? a) int b) float c) void d) double Answer: c Explanation: Return type of an method must be made void if it is not returning any value. 2. What is the process of defining more than one method in a class differentiated by method signature? a) Function overriding b) Function overloading c) Function doubling d) None of the mentioned Answer:b Explanation: Function overloading is a process of defining more than one method in a class with same name differentiated by function signature i:e return type or parameters type and number. Example – int volume(int length, int width) & int volume(int length , int width , int height) can be used to calculate volume. 3. Which of the following is a method having same name as that of it’s class? a) finalize b) delete c) class d) constructor Answer: d Explanation: A constructor is a method that initializes an object immediately upon creation. It has the same name as that of class in which it resides. 4. Which method can be defined only once in a program? a) main method b) finalize method c) static method d) private method Answer: a Explanation: main() method can be defined only once in a program. Program execution begins from the main() method by java’s run time system. 5. Which of these statement is incorrect? a) All object of a class are allotted memory for the all the variables defined in the class. b) If a function is defined public it can be accessed by object of other class by inheritation. c) main() method must be made public. d) All object of a class are allotted memory for the methods defined in the class. Answer: d Explanation: All object of class share a single copy of methods defined in a class, Methods are allotted memory only once. All the objects of the class have access to methods of that class are allotted memory only for the variables not for the methods. 6. What is the output of this program? 1. class box { 2. int width; 3. int height; 4. int length; 5. int volume; 6. void volume(int height, int length, int width) { 7. volume = width*height*length; 8. } 9. } 10. class Prameterized_method{ 11. public static void main(String args[]) 12. { 13. box obj = new box(); 14. obj.height = 1; 15. obj.length = 5; 16. obj.width = 5; 17. obj.volume(3,2,1); 18. System.out.println(obj.volume); 19. } 20. } a) 0 b) 1 c) 6 d) 25 Answer: c Explanation: None. output: $ Prameterized_method.java $ Prameterized_method 6 7. What is the output of this program? 1. class equality { 2. int x; 3. int y; 4. boolean isequal(){ 5. return(x == y); 6. } 7. } 8. class Output { 9. public static void main(String args[]) 10. { 11. equality obj = new equality(); 12. obj.x = 5; 13. obj.y = 5; 14. System.out.println(obj.isequal); } 15. } a) false b) true c) 0 d) 1 Answer: b Explanation: None. output: $ javac Output.java $ java Output true 8. What is the output of this program? 1. class box { 2. int width; 3. int height; 4. int length; 5. int volume; 6. void volume() { 7. volume = width*height*length; 8. } 9. } 10. class Output { 11. public static void main(String args[]) 12. { 13. box obj = new box(); 14. obj.height = 1; 15. obj.length = 5; 16. obj.width = 5; 17. obj.volume(); 18. System.out.println(obj.volume); 19. } 20. } a) 0 b) 1 c) 25 d) 26 Answer:c Explanation: None. output: $ javac Output.java $ java Output 25 9. What is the output of this program? 1. class Output { 2. static void main(String args[]) 3. { 4. int x , y = 1; 5. x = 10; 6. if (x != 10 && x / 0 == 0) 7. System.out.println(y); 8. else 9. System.out.println(++y); 10. } 11. } a) 1 b) 2 c) Runtime Error d) Compilation Error Answer: d Explanation: main() method must be made public. Without main() being public java run time system will not be able to access main() and will not be able to execute the code. output: $ javac Output.java Error: Main method not found in class Output, please define the main method as: public static void main(String[] args) 10. What is the output of this program? 1. class area { 2. int width; 3. int length; 4. int volume; 5. area() { 6. width=5; 7. length=6; 8. } 9. void volume() { 10. volume = width*height*length; 11. } 12. } 13. class cons_method { 14. public static void main(String args[]) 15. { 16. area obj = new area(); 17. obj.volume(); 18. System.out.println(obj.volume); 19. } 20. } a) 0 b) 1 c) 25 d) 30 Answer: d Explanation: None. output: $ javac cons_method.java $ java cons_method 30 Constructors & Garbage Collection ====================== 1. What is the return type of Constructors? a) int b) float c) void d) None of the mentioned Answer: d Explanation: Constructors does not have any return type, not even void. 2. Which keyword is used by method to refer to the object that invoked it? a) import b) catch c) abstract d) this Answer: d Explanation: this keyword can be used inside any method to refer to the current object. this is always a reference to the object on which the method was invoked. 3. Which of the following is a method having same name as that of its class? a) finalize b) delete c) class d) constructor Answer: d Explanation: A constructor is a method that initializes an object immediately upon creation. It has the same name as that of class in which it resides. 4. Which operator is used by Java run time implementations to free the memory of an object when it is no longer needed? a) delete b) free c) new d) None of the mentioned Answer: d Explanation: Java handles deallocation of memory automatically, we do not need to explicitly delete an element. Garbage collection only occurs during execution of the program. When no references to the object exist, that object is assumed to be no longer needed, and the memory occupied by the object can be reclaimed. 5. Which function is used to perform some action when the object is to be destroyed? a) finalize() b) delete() c) main() d) None of the mentioned Answer: a Explanation: None. 6. What is the output of this program? 1. class box { 2. int width; 3. int height; 4. int length; 5. int volume; 6. box() { 7. width = 5; 8. height = 5; 9. length = 6; 10. } 11. void volume() { 12. volume = width*height*length; 13. } 14. } 15. class constructor_output { 16. public static void main(String args[]) 17. { 18. box obj = new box(); 19. obj.volume(); 20. System.out.println(obj.volume); } 21. } 22. } a) 100 b) 150 c) 200 d) 250 Answer: b Explanation: None. output: $ constructor_output.java $ constructor_output 150 7. What is the output of this program? 1. class equality { 2. int x; 3. int y; 4. boolean isequal() { 5. return(x == y); 6. } 7. } 8. class Output { 9. public static void main(String args[]) 10. { 11. equality obj = new equality(); 12. obj.x = 5; 13. obj.y = 5; 14. System.out.println(obj.isequal); } 15. } a) false b) true c) 0 d) 1 Answer: b Explanation: None. output: $ javac Output.java $ java Output true 8. What is the output of this program? 1. class box { 2. int width; 3. int height; 4. int length; 5. int volume; 6. void finalize() { 7. volume = width*height*length; 8. System.out.println(volume); 9. } 10. protected void volume() { 11. volume = width*height*length; 12. System.out.println(volume); 13. } 14. } 15. class Output { 16. public static void main(String args[]) 17. { 18. box obj = new box(); 19. obj.volume(); 20. } 21. } a) 150 b) 200 c) Runtime error d) Compilation error Answer: a Explanation: None. output: $ javac Output.java $ java Output 150 9. Which of the folowing stements are incorrect? a) Default constructor is called at the time of declaration of the object if a constructor has not been defined. b) Constructor can be parameterized. c) finalize() method is called when a object goes out of scope and is no longer needed. d) finalize() method must be declared protected. Answer: c Explanation: finalize() method is called just prior to garbage collection. it is not called when object goes out of scope. 10. What is the output of this program? 1. class area { 2. int width; 3. int length; 4. int area; 5. void area(int width, int length) { 6. this.width = width; 7. this.length = length; 8. } 9. 10. } 11. class Output { 12. public static void main(String args[]) 13. { 14. area obj = new area(); 15. obj.area(5 , 6); 16. System.out.println(obj.length + " " + obj.width); 17. } 18. } a) 0 0 b) 5 6 c) 6 5 d) 5 5 Answer: c Explanation: this keyword can be used inside any method to refer to the current object. this is always a reference to the object on which the method was invoked. output: $ javac Output.java $ java Output 6 5 Overloading Methods & Argument Passing ==================== 1. What is process of defining two or more methods within same class that have same name but different parameters declaration? a) method overloading b) method overriding c) method hiding d) None of the mentioned Answer: a Explanation: Two or more methods can have same name as long as their parameters declaration is different, the methods are said to be overloaded and process is called method overloading. Method overloading is a way by which Java implements polymorphism. 2. Which of these can be overloaded? a) Methods b) Constructors c) Both a & b d) None of the mentioned Answer: c Explanation: None. 3. Which of these is correct about passing an argument by call-by-value process? a) Copy of argument is made into the formal parameter of the subroutine. b) Reference to original argument is passed to formal parameter of the subroutine. c) Copy of argument is made into the formal parameter of the subroutine and changes made on parameters of subroutine have effect on original argument. d) Reference to original argument is passed to formal parameter of the subroutine and changes made on parameters of subroutine have effect on original argument. Answer: a Explanation: When we pass an argument by call-by-value a copy of argument is made into the formal parameter of the subroutine and changes made on parameters of subroutine have no effect on original argument, they remain the same. 4. What is the process of defining a method in terms of itself, that is a method that calls itself? a) Polymorphism b) Abstraction c) Encapsulation d) Recursion Answer: d Explanation: None. 5. Which of the following statements are incorrect? a) Default constructor is called at the time of declaration of the object if a constructor has not been defined. b) Constructor can be parameterized. c) finalize() method is called when a object goes out of scope and is no longer needed. d) finalize() method must be declared protected. Answer: c Explanation: finalize() method is called just prior to garbage collection. It is not called when object goes out of scope. 6. What is the output of this program? 1. class overload { 2. int x; 3. int y; 4. int add(int a) { 5. x = a + 1; 6. } 7. int add(int a , int b){ 8. x = a + 2; 9. } 10. } 11. class Overload_methods { 12. public static void main(String args[]) 13. { 14. overload obj = new overload(); 15. int a = 0; 16. obj.add(6); 17. System.out.println(obj.x); 18. } 19. } a) 5 b) 6 c) 7 d) 8 Answer: c Explanation: None. output: $ javac Overload_methods.java $ java Overload_methods 7 7. What is the output of this program? 1. class overload { 2. int x; 3. int y; 4. int add(int a){ 5. x = a + 1; 6. } 7. int add(int a , int b){ 8. x = a + 2; 9. } 10. } 11. class Overload_methods { 12. public static void main(String args[]) 13. { 14. overload obj = new overload(); 15. int a = 0; 16. obj.add(6, 7); 17. System.out.println(obj.x); 18. } 19. } a) 6 b) 7 c) 8 d) 9 Answer: c Explanation: None. output: $ javac Overload_methods.java $ java Overload_methods 8 8. What is the output of this program? 1. class overload { 2. int x; 3. double y; 4. int add(int a , int b) { 5. x = a + b; 6. } 7. int add(double c , double d){ 8. y = c + d; 9. } 10. overload() { 11. this.x = 0; 12. this.y = 0; 13. } 14. } 15. class Overload_methods { 16. public static void main(String args[]) 17. { 18. overload obj = new overload(); 19. int a = 2; 20. double b = 3.2; 21. obj.add(a, a); 22. obj.add(b, b); 23. System.out.println(obj.x + " " + obj.y); 24. } 25. } a) 6 6 b) 6.4 6.4 c) 6.4 6 d) 6 6.4 Answer: d Explanation: None. output: $ javac Overload_methods.java $ java Overload_methods 6 6.4 9. What is the output of this program? 1. class test { 2. int a; 3. int b; 4. void meth(int i , int j) { 5. i *= 2; 6. j /= 2; 7. } 8. } 9. class Output { 10. public static void main(String args[]) 11. { 12. test obj = new test(); 13. int a = 10; 14. int b = 20; 15. obj.meth(a , b); 16. System.out.println(a + " " + b); 17. } 18. } a) 10 20 b) 20 10 c) 20 40 d) 40 20 Answer: a Explanation: Variables a & b are passed by value, copy of their values are made on formal parameters of function meth() that is i & j. Therefore changes done on i & j are not reflected back on original arguments. a & b remain 10 & 20 respectively. output: $ javac Output.java $ java Output 10 20 10. What is the output of this program? 1. class test { 2. int a; 3. int b; 4. test(int i, int j) { 5. a = i; 6. b = j; 7. } 8. void meth(test o) { 9. o.a *= 2; 10. O.B /= 2; 11. } 12. } 13. class Output { 14. public static void main(String args[]) 15. { 16. test obj = new test(10 , 20); 17. obj.meth(obj); 18. System.out.println(obj.a + " " + obj.b); 19. } 20. } a) 10 20 b) 20 10 c) 20 40 d) 40 20 Answer: c Explanation: class objects are always passed by reference, therefore changes done are reflected back on original arguments. obj.meth(obj) sends object obj as parameter whose variables a & b are multiplied and divided by 2 respectively by meth() function of class test. a & b becomes 20 & 40 respectively. output: $ javac Output.java $ java Output 20 40 Inheritance ===== . 1. Which of these keyword must be used to inherit a class? a) super b) this c) extent d) extends View Answer Answer: d Explanation: None. 2. Which of these keywords is used to refer to member of base class from a sub class? a) upper b) super c) this d) None of the mentioned Answer: b Explanation: whenever a subclass needs to refer to its immediate superclass, it can do so by use of the keyword super. 3. A class member declared protected becomes member of subclass of which type? a) public member b) private member c) protected member d) static member Answer: b Explanation: A class member declared protected becomes private member of subclass. 4. Which of these is correct way of inheriting class A by class B? a) class B + class A {} b) class B inherits class A {} c) class B extends A {} d) class B extends class A {} Answer: c Explanation: None. 5. Which of the following statements are incorrect? a) public members of class can be accessed by any code in the program. b) private members of class can only be accessed by other members of the class. c) private members of class can be inherited by a sub class, and become protected members in sub class. d) protected members of a class can be inherited by a sub class, and become private members of the sub class. Answer: c Explanation: private members of a class cannot be inherited by a sub class. 6. What is the output of this program? 1. class A { 2. int i; 3. void display() { 4. System.out.println(i); 5. } 6. } 7. class B extends A { 8. int j; 9. void display() { 10. System.out.println(j); 11. } 12. } 13. class inheritance_demo { 14. public static void main(String args[]) 15. { 16. B obj = new B(); 17. obj.i=1; 18. obj.j=2; 19. obj.display(); 20. } 21. } a) 0 b) 1 c) 2 d) Compilation Error Answer: c Explanation: class A & class B both contain display() method, class B inherits class A, when display() method is called by object of class B, display() method of class B is executed rather than that of Class A. output: $ javac inheritance_demo.java $ java inheritance_demo 2 7. What is the output of this program? 1. class A { 2. int i; 3. } 4. class B extends A { 5. int j; 6. void display() { 7. super.i = j + 1; 8. System.out.println(j + " " + i); 9. } 10. } 11. class inheritance { 12. public static void main(String args[]) 13. { 14. B obj = new B(); 15. obj.i=1; 16. obj.j=2; 17. obj.display(); 18. } 19. } a) 2 2 b) 3 3 c) 2 3 d) 3 2 Answer: c Explanation: None output: $ javac inheritance.java $ java inheritance 2 3 8. What is the output of this program? 1. class A { 2. public int i; 3. private int j; 4. } 5. class B extends A { 6. void display() { 7. super.j = super.i + 1; 8. System.out.println(super.i + " " + super.j); 9. } 10. } 11. class inheritance { 12. public static void main(String args[]) 13. { 14. B obj = new B(); 15. obj.i=1; 16. obj.j=2; 17. obj.display(); 18. } 19. } a) 2 2 b) 3 3 c) Runtime Error d) Compilation Error Answer: d Explanation: class contains a private member variable j, this cannot be inherited by subclass B and does not have access to it. output: $ javac inheritance.java Exception in thread “main” java.lang.Error: Unresolved compilation problem: The field A.j is not visible 9. What is the output of this program? 1. class A { 2. public int i; 3. public int j; 4. A() { 5. i = 1; 6. j = 2; 7. } 8. } 9. class B extends A { 10. int a; 11. B() { 12. super(ob); 13. } 14. } 15. class super_use { 16. public static void main(String args[]) 17. { 18. B obj = new B(); 19. System.out.println(obj.i + " " + obj.j) 20. } 21. } a) 1 2 b) 2 1 c) Runtime Error d) Compilation Error Answer: a Explanation: Keyword super is used to call constructor of class A by constructor of class B. Constructor of a initializes i & j to 1 & 2 respectively. output: $ javac super_use.java $ java super_use 1 2 10. What is the output of this program? 1. class A { 2. public int i; 3. protected int j; 4. } 5. class B extends A { 6. int j; 7. void display() { 8. super.j = 3; 9. System.out.println(i + " " + j); 10. } 11. } 12. class Output { 13. public static void main(String args[]) 14. { 15. B obj = new B(); 16. obj.i=1; 17. obj.j=2; 18. obj.display(); 19. } 20. } a) 1 2 b) 2 1 c) 1 3 d) 3 1 Answer: a Explanation: Both class A & B have member with same name that is j, member of class B will be called by default if no specifier is used. I contains 1 & j contains 2, printing 1 2. output: $ javac Output.java $ java Output 1 2
Posted on: Mon, 16 Sep 2013 23:41:06 +0000

Trending Topics



Recently Viewed Topics




© 2015