5
EX NO: 06 JAVA APTITUDE QUESTIONS Date: 02.03.2009 1) 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 Ans: (a) Explanation: The code compiles and runs fine and the ‘if’ test succeeds because ‘var’ is set to ‘true’ (rather than tested for ‘true’) in the ‘if’ argument. 2) Vector is declared as follows. What happens if the code tried to add 6 th element to this Vector new vector(5,10) (a) The element will be successfully added (b) ArrayIndexOutOfBounds Exception (c) The Vector allocates space to accommodate up to 15 elements Ans: (a) and (c) Explanation: The 1 st argument in the constructor is the initial size of Vector and the 2 nd argument in the constructor is the growth in size (for each allocation) This Vector is created with 5 elements and when an extra element (6 th one) is tried to be added, the vector grows in size by 10. 3) What can go wrong if you replace && with & in the following code: String a=null; if (a!=null && a.length()>10) {...} Ans: A single ampersand here would lead to a NullPointerException.

Ex No: 06 Java Aptitude Questions Date:

  • Upload
    rama

  • View
    258

  • Download
    1

Embed Size (px)

DESCRIPTION

java apti questions

Citation preview

Page 1: Ex No: 06 Java Aptitude Questions Date:

EX NO: 06JAVA APTITUDE QUESTIONS

Date: 02.03.2009

1) 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 ErrorAns: (a)Explanation: The code compiles and runs fine and the ‘if’ test succeeds because ‘var’ is set to ‘true’ (rather than tested for ‘true’) in the ‘if’ argument.

2) Vector is declared as follows. What happens if the code tried to add 6 th element to this Vectornew vector(5,10)

(a) The element will be successfully added(b) ArrayIndexOutOfBounds Exception(c) The Vector allocates space to accommodate up to 15 elementsAns: (a) and (c)Explanation: The 1 st argument in the constructor is the initial size of Vector and the 2 nd argument in the constructor is the growth in size (for each allocation)This Vector is created with 5 elements and when an extra element (6 th one) is tried to be added, the vector grows in size by 10.

3) What can go wrong if you replace && with & in the following code:String a=null;if (a!=null && a.length()>10){...}

Ans: A single ampersand here would lead to a NullPointerException.

4) Which are keywords in Java?a) NULLb) sizeofc) friendd) extendse) synchronizedAns: d and e

Page 2: Ex No: 06 Java Aptitude Questions Date:

Reg No: 30906205043Date: 02.03.2009

5) The control expression in an "if" statement must be:1. an expression with type boolean2. an expression with either the type boolean or integer3. an expression with type integer4. an expression with either the type boolean or integer with value 0 or 1Ans: (3)Explanation: In the Java programming language, only expressions of type boolean are allowed as control expressions in an "if" statement.

6) What is the difference between an array and a vector?

Number of elements in an array are fixed at the construction time, whereas the number of elements in vector can grow dynamically.

7) What is a constructor?

In Java, the class designer can guarantee initialization of every object by providing a special method called a constructor. If a class has a constructor, Java automatically calls that constructor when an object is created, before users can even get their hands on it. So initialization is guaranteed.

8) What is meant by final class, methods and variables?

This modifier can be applied to class method and variable. When declared as final class the class cannot be extended. When declared as final variable, its value cannot be changed if is primitive value, if it is a reference to the object it will always refer to the same object, internal attributes of the object can be changed.

9) Consider the following code:int i = 1;switch(c)

1) output will be One, followed by Two, and then followed by Three2) code is illegal and therefore will not compile3) output will be One followed by Two4) output will be One

Ans: 1)Explanation: The flow of control in switch statements goes subsequently through the case statements unless explicitly broken using the break statement. In which case, the flow transfers to the first statement after the switch block. Without any break statements, all the cases are executed.

10) What is an example of polymorphism?Inner class Anonymous classes Method overloading Method overridingAns : c

Page 3: Ex No: 06 Java Aptitude Questions Date:

Reg No: 30906205043Date: 02.03.2009

11) User-defined package can also be imported just like the standard packages.True/FalseAns : True

12) When a program does not want to handle exception, the ______class is used.Ans :Throws

13) The main subclass of the Exception class is _______ class.Ans :RuntimeException

14) Only subclasses of ______class may be caught or thrown.Ans :Throwable

15) Any user-defined exception class is a subclass of the _____ class.Ans :Exception

16) Consider the following code:class NewString extends java.lang.String   

1) Compiles successfully2) Results in error because String is abstract3) Results in error because class body is not defined4) Results in error because the class is not declaredpublic5) Results in error because java.lang.String is finalAns :5)Explanation: java.lang.String is a final class and cannot be extended.

17) Are there any errors in the following class definition?abstract class Class1 

1. Class header definition is wrong2. Method definition is wrong3. Constructor needs to be defined4. No errorsAns :2)Explanation: The method func1, which is declared as abstract cannot have a body as in the code above

18) Which of the following statements about abstract methods/classes in java are true?1. An abstract class cannot be instantiated.2. Constructors cannot be abstract.3. A subclass of an abstract class must defined the abstract methods.4. Static methods may be declared abstract.

1. Line 1, line 2 and line 3 only2. Line 1 only3. Line 1 and line 2 only4. Line 2 only5. All are true

Page 4: Ex No: 06 Java Aptitude Questions Date:

Reg No: 30906205043Date: 02.03.2009

Ans : choice 3

Explanation: A subclass of an abstract class can also be abstract if it does not define all the abstract methods in the parent class.

19) How to declare an interface example?Ans : access class classname implements interface.

20) Can you achieve multiple interface through interface?a)True b) falseAns : a.