Sunteți pe pagina 1din 45

1. Which of the following methods are overloaded with respect to one another?

public int max (int a, int b) { … }


public double max (double a, double b) { … }
public int max (int a, int b, int c) { … }
public double max (double a, double b, double c) { … }

A. A and B are overloaded; C and D are overloaded.

B. A and C are overloaded; B and D are overloaded.

C. A, B and C are overloaded.

D. All four methods are overloaded.

2. When a subclass constructor calls its superclass constructor, what happens if the superclass’s
constructor does not assign a value to an instance variable?

A. A syntax error occurs.

B. A compile-time error occurs.

C. A run-time error occurs.

D. The program compiles and runs because the instance variables are initialized to their
default values.

3. When overriding a superclass method and calling the superclass version from the subclass method,
failure to prefix the superclass method name with the keyword super and a dot (.) in the superclass
method call causes ________.

A. a compile-time error.

B. a syntax error.

C. infinite recursion.

D. a runtime error.

4. Which statement is true when a superclass has protected instance variables?

A. A subclass object can assign an invalid value to the superclass’s instance variables, thus
leaving an object in an inconsistent state.

B. Subclass methods are more likely to be written so that they depend on the superclass’s
data implementation.

C. We may need to modify all the subclasses of the superclass if the superclass
implementation changes.

D. All of the above.

5. Which superclass members are inherited by all subclasses of that superclass?

A. private instance variables and methods.


B. protected instance variables and methods.

C. private constructors.

D. protected constructors.

6. Consider the classes below, declared in the same file:

class A
{
int a;
public A()
{
a = 7;
}
}

class B extends A
{
int b;
public B()
{
b = 8;
}
}

Which of the statements below is false?


A. Both variables a and b are instance variables.

B. After the constructor for class B executes, the variable a will have the value 7.

C. After the constructor for class B executes, the variable b will have the value 8.

D. A reference of type A can be treated as a reference of type B.

7. Overriding a method differs from overloading a method because:

A. Overloaded methods have the same signature.

B. Overridden methods have the same signature.

C. Both of the above.

D. Neither of the above.

8. The default equals implementation of class Object determines:

A. whether two references refer to the same object in memory.

B. whether two references have the same type.

C. whether two objects have the same instance variables.

D. whether two objects have the same instance variable values.


9. Every class in Java, except ________, extends an existing class.

A. Integer.

B. Object.

C. String.

D. Class.

10. Which of the following statements is false?

A. A class can directly inherit from class Object.

B. It's often much more efficient to create a class by inheriting from a similar class than to
create the class by writing every line of code the new class requires.

C. If the class you're inheriting from declares instance variables as private, the inherited
class can access those instance variables directly.

D. A class's instance variables are normally declared private to enforce good software
engineering.

11. Which of the following statements is false?

A. A subclass is often larger than its superclass.

B. A superclass object is a subclass object.

C. The class following the extends keyword in a class declaration is the direct superclass of
the class being declared.

D. Java uses interfaces to provide the benefits of multiple inheritance.

12. Using the protected keyword also gives a member:

A. public access.

B. package access.

C. private access.

D. block scope.

13. Which statement best describes the relationship between superclass and subclass types?

A. A subclass reference cannot be assigned to a superclass variable and a superclass


reference cannot be assigned to a subclass variable.

B. A subclass reference can be assigned to a superclass variable and a superclass reference


can be assigned to a subclass variable.

C. A superclass reference can be assigned to a subclass variable, but a subclass reference


cannot be assigned to a superclass variable.
D. A subclass reference can be assigned to a superclass variable, but a superclass reference
cannot be assigned to a subclass variable.

14. If the superclass contains only abstract method declarations, the superclass is used for ________.

A. implementation inheritance.

B. interface inheritance.

C. Both.

D. Neither.

15. Which of the following statements about abstract superclasses is true?

A. abstract superclasses may contain data.

B. abstract superclasses may not contain implementations of methods.

C. abstract superclasses must declare all methods as abstract.

D. abstract superclasses must declare all data members not given values as abstract.

16. Consider the abstract superclass below:

public abstract class Foo


{
private int a;
public int b;

public Foo(int aVal, int bVal)


{
a = aVal;
b = bVal;
}

public abstract int calculate();


}
Any concrete subclass that extends class Foo:
A. Must implement a method called calculate.

B. Will not be able to access the instance variable a.

C. Neither (a) nor (b).

D. Both (a) and (b).

17. Assigning a subclass reference to a superclass variable is safe ________.

A. because the subclass object has an object of its superclass.

B. because the subclass object is an object of its superclass.

C. only when the superclass is abstract.

D. only when the superclass is concrete.


18. Consider classes A, B and C, where A is an abstract superclass, B is a concrete class that inherits from
A and C is a concrete class that inherits from B. Class A declares abstract method originalMethod,
implemented in class B. Which of the following statements is true of class C?

A. Method originalMethod cannot be overridden in class C—once it has been implemented


in concrete class B, it is implicitly final.

B. Method originalMethod must be overridden in class C, or a compilation error will occur.

C. If method originalMethod is not overridden in class C but is called by an object of class


C, an error occurs.

D. None of the above.

19. Which keyword is used to specify that a class will define the methods of an interface?

A. uses

B. implements

C. defines

D. extends

20. Which of the following is not possible?

A. A class that implements two interfaces.

B. A class that inherits from two classes.

C. A class that inherits from one class, and implements an interface.

D. All of the above are possible.

21. Which of the following statements is false?

A. Prior to Java SE 8, it was common to associate with an interface a class containing static
helper methods for working with objects that implemented the interface.

B. Class Collections contains many static helper methods for working with objects that
implement interfaces Collection, List, Set and more.

C. Collections method sort can sort objects of any class that implements interface List.

D. With non-static interface methods, helper methods can now be declared directly in
interfaces rather than in separate classes.

22. Which of the following statements is true?

A. The throw statement is used to throw an exception.

B. The throw statement is used to specify that a method will throw an exception.

C. The throw statement is used to access an exception parameter.

D. All of the above.


23. Which of the following statements is true?

A. The code in a finally block is executed only if an exception occurs.

B. The code in a finally block is executed only if an exception does not occur.

C. The code in a finally block is executed only if there are no catch blocks.

D. None of the above are true.

24. What is the difference between a try block and a try statement?

A. There is no difference; the terms can be used interchangeably.

B. A try statement refers to the block of code following the keyword try, while the try block
refers to the try keyword and the block of code following this keyword.

C. The try block refers to the keyword try followed by a block of code. The try block and its
corresponding catch and/or finally clauses together form a try statement.

D. The try statement refers to the keyword try followed by a block of code. The try
statement and its corresponding catch and/or finally clauses together form a try block.

25. In the catch block below, what is e?

catch (ArithmeticException e)
{
System.out.println(e);
}

A. The type of the exception being caught.

B. The name of catch block’s exception parameter.

C. A finally block.

D. An exception handler.

26. All exception classes inherit, either directly or indirectly, from ________.

A. class Error.

B. class RuntimeException.

C. class Throwable.

D. None of the above.

27. When must a program explicitly use the this reference?

A. Accessing a private variable.

B. Accessing a public variable.

C. Accessing a local variable.

D. Accessing an instance variable that is shadowed by a local variable.


28. A programmer-defined constructor that has no arguments is called a(n) ________.

A. empty constructor.

B. no-argument constructor.

C. default constructor.

D. null constructor.

29. Which of the following class members should usually be private?

A. Methods.

B. Constructors.

C. Variables (or fields).

D. All of the above.

30. What will be the output of following code segment?

class A {
int i;
void display() {
System.out.println(i);
}
}
class B extends A {
int j;
void display() {
System.out.println(j);
}
}
class inheritance_demo {
public static void main(String args[])
{
B obj = new B();
obj.i=1;
obj.j=2;
obj.display();
}
}

A. 0

B. 1

C. 2

D. Compilation Error

31. What is the output of this program?

class A {
public int i;
protected int j;
}
class B extends A {
int j;
void display() {
super.j = 3;
System.out.println(i + " " + j);
}
}
class Output {
public static void main(String args[])
{
B obj = new B();
obj.i=1;
obj.j=2;
obj.display();
}
}

A. 1 2

B. 2 1

C. 1 3

D. 3 1

32. What is the output of this program?

class access{
public int x;
private int y;
void cal(int a, int b){
x = a + 1;
y = b;
}
}
class access_specifier {
public static void main(String args[])
{
access obj = new access();
obj.cal(2, 3);
System.out.println(obj.x + " " + obj.y);
}
}

A. 3 3

B. 2 3

C. Runtime Error

D. Compilation Error

33. What is the error in the following class definitions?

abstract class XY
{
abstract sum(int x, int y){ }
}

A. Class header is not defined properly

B. Constructor is not defined

C. Method is not defined properly

D. No error.

34. Which of the following variables contains null reference?

I. String a;
II. String b = new String();
III. String c = "";
IV. String d = "null";

A. I

B. II

C. III

D. IV

35. Which of the following is correct syntax for defining a new class Movie based on the superclass
Multimedia?

A. class Movie isa Multimedia { //additional definitions go here }

B. class Movie implements Multimedia { //additional definitions go here }

C. class Movie defines Multimedia { //additional definitions go here }

D. class Movie extends Multimedia { //additional definitions go here }

36. The concept of multiple inheritance is implemented in Java by

I. Extending two or more classes.


II. Extending one class and implementing one or more interfaces.
III. Implementing two or more interfaces.
A. II

B. I and II

C. II and III

D. I

E. III

37. Which of the following is true about inheritance in Java?

I) Private methods are final.


II) Protected members are accessible within a package and inherited classes outside the package.
III) Protected methods are final.
IV) We cannot override private methods.
A. I, II and IV

B. Only I and II

C. I, II and III

D. II, III and IV

38. Which is true?

A. "X extends Y" is correct if and only if X is a class and Y is an interface

B. "X extends Y" is correct if and only if X is an interface and Y is a class

C. "X extends Y" is correct if X and Y are either both classes or both interfaces

D. "X extends Y" is correct for all combinations of X and Y being classes and/or
interfaces

39. Inheritance is also known as the

A. knows-a relationship.

B. has-a relationship.

C. uses-a relationship.

D. is-a relationship.

40. Which of the following keywords allows a subclass to access a superclass method even when the
subclass has overridden the superclass method?

A. base.

B. super.

C. this.

D. public.
1. Assume Program1.java file is already compiled, which command is correct to
run the program?
A. start Program1.java
B. run Program1
C. java Program1.java
D. java Program1

2. Which signatures is valid for the main() method to work as entry point of Java
application?
A. public static void main()
B. public static void main(String arg[])
C. public static int main(String [] arg)
D. public static String[] main(String[] arg)

3. What is valid declaration of a float?


A. float f = 1F;
B. float f = 1.0;
C. float f = "1";
D. float f = 1.0d;

4. What is the maximum number that can be stored in long type variable?
A. 264
B. 264 – 1
C. 263
D. 263-1

5. Which keyword indicates that a method does not return a value?


A. null
B. void
C. 0
D. no-value

6. Which of the following is true about instance variables?


A. Instance variables are used to improve the performance
B. All objects of the class share one copy of instance variable, so it uses
less memory
C. Each object has its own copy of Instance variables
D. Instance variables are same as static, they just use less memory

7. Consider the following application:


1. class Q6 {
2. public static void main(String args[]) {
3. Holder h = new Holder();
4. h.held = 100;
5. h.bump();
6. System.out.println(h.held);
7. }
8. }
9.
10. class Holder {
11. public int held;
12. public void bump() {
13. int num = held;
14 num++;
15. }
16. }

What value would be printed at line 6?


A. 0
B. 1
C. 100
D. 101

8. What does the following code print?

public class A {
static int x;
public static void main(String[] args) {
A that1 = new A();
A that2 = new A();
that1.x = 5;
that2.x = 1000;
x = -1;
System.out.println(that2.x);
}
}

A. 0
B. 5
C. 1000
D. -1

9. Which statement is true about this application?


1. class StaticStuff
2 {
3. static int x = 10;
4.
5. static { x += 5; }
6.
7. public static void main(String args[])
8. {
9. System.out.println("x = " + x);
10. }
11.
12. static {x /= 5; }
13. }

A. Lines 5 and 12 will not compile because the method names and return
types are missing.
B. Line 12 will not compile because you can only have one static
initializer.
C. The code compiles and execution produces the output x = 10.
D. The code compiles and execution produces the output x = 15.
E. The code compiles and execution produces the output x = 3.

10.You want to create a table that looks like:


12 -9 8
7 14
-32 -1 0

A. double[][] table =
{ 12, -9, 8,
7, 14,
-32, -1, 0} ;
B. double[][] table =
{ {12, -9, 8},
{7, 14, 0},
-32, -1, 0} };
C. double[][] table =
{ {12, -9, 8}
{7, 14}
{-32, -1, 0} };
D. double[][] table =
{ {12, -9, 8},
{7, 14},
{-32, -1, 0} };

11.Which of following statement is true in context of static methods and static


variables?
A. static variables can’t be accessed in non-static methods
B. static methods can’t be called from non-static methods
C. static methods can be called from non-static methods
D. you can access instance variables in static methods using this keyword

12.A variables declared within a method are called?


A. Instance variables
B. Local variables
C. Final variables
D. Static variables

13.Why we use get and set methods to read and write instance variables?
A. We can’t access instance variables without get/set methods
B. JVM performance is effected when we access instance variables
directly
C. Get/set methods allows to impose constraints before reading or writing
D. Get/set methods increase the program speed when called correctly

14.Assume you need to declare an instance attribute that should be editable


from the outside i.e. client-code may need to change its value. How you
would decide whether to encapsulate it?
A. Making it public is better option to make the code easy, there is no
need to encapsulate
B. I would encapsulate only if there is some rule (when the class was
declared) that must be verified before the changing the value of that
instance attribute
C. Program consume less memory to store encapsulated instance
attributes
D. I would encapsulate because we may need to impose some rule on
data in future

15.What is prototype of default constructor for a class named Test?


A. public Test(void)
B. Test( )
C. Test(void)
D. public Test( )
E. None of these

16.Types in Java are divided into two categories ______ types and ________ types.
A. Object and Variable
B. Class and Object
C. Primitives and Classes
D. Primitive and Reference

17.What is return type of constructor?


A. int
B. float
C. void
D. None of these

18.Which of following is a method having same name as that of its class?


A. finalize
B. constructor
C. delete
D. class

19.When multiple constructors are defined inside a class, how compiler


differentiate overloaded constructors?
A. By counting the number of constructors
B. Using constructor signatures
C. Counting number of lines of code inside each constructor
D. Analyzing the difference of code written in constructors body

20.Assume two constructors are defined with different signatures but they
contain exactly same code in their body. Which of following statement is
correct?
A. Different constructors are used to initialize object in different ways, if
both constructors have same code, its compile time error
B. Code would compile successfully but at runtime, code would break or
terminate
C. There is no compile-time or runtime issue even if both constructors’
body is same
D. Code would compile but at runtime, unexpected result would be
generated

21.Assume you declared only one constructor that takes an argument. While
creating the object of that class, you passed no argument to constructor.
Choose most appropriate option.
A. Its compile-time error because no-argument constructor is not defined
B. Code is correct as compiler provide default constructor that is a no-
argument constructor
C. Code would not run even if you define no-argument constructor
because only compiler can provide no-argument constructor
D. This is an error, because all classes must contain more than one
constructors
22.It’s possible to have several methods with the same name where method
operate on different types or numbers of arguments. This feature is called?
A. Encapsulation
B. Method Overloading
C. Constructor Overloading
D. Method Overriding

23.Which method is called implicitly when an object appears in code when a


String is needed?
A. String()
B. toString()
C. printString()
D. print()

24. Composition is sometimes referred to as?


A. Has-A relation
B. Is-A relation
C. Belongs-To relation
D. Related-To relation

25.Which keyword is used to declare a variable whose value do not change once
it is initialized at the point of declaration?
A. static
B. final
C. private
D. fix

26.In Java, arrays are ________________?


A. Objects
B. Object reference
C. Primitive data types
D. None of the above

27.What would be the output of following program?


public class Test{
public static void main(String[] args){
int[] x = new int[3];
System.out.println("x[0] is " + x[0]);
}
}

A. The program has a compile error because the size of the array wasn't
specified when declaring the array.
B. The program has a runtime error because the array elements are not
initialized.
C. The program runs fine and displays x[0] is 0
D. The program has a runtime error because the array element x[0] is not
defined

28.Analyze the following code and choose the correct answer.

int[] arr = new int[5];


arr = new int[6];

A. The code has compile errors because the variable arr cannot be
changed once it is assigned.
B. The code would compile and run fine. The second line assigns a new
array to arr.
C. The code has compile errors because we cannot assign a different size
array to arr.
D. It compile successfully if we also change the size of second array from
6 to 5

29.When you pass an array to a method, the method receives:


A. A copy of the array
B. A copy of the first element
C. The reference of the array
D. The length of the array

30.What is the value of a[1] after the following code is executed?


int[] a = {0, 2, 4, 1, 3};
for(int i = 0; i < a.length; i++)
a[i] = a[(a[i] + 3) % a.length];

A. 0
B. 1
C. 2
D. 3
Section 1- Multiple choice questions.

1. What is the return type of Constructors?


a) int
b) float
c) void
d) None of the mentioned

2. Which keyword is used by method to refer to the object that invoked it?
a) import
b) catch
c) abstract
d) this

3. 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

4. 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. }
16. }

a) false
b) true
c) 0
d) 1
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.

6. 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

7. Which of this access specifiers can be used for a class so that its members can be accessed by a
different class in the same package?
a) Public
b) Protected
c) No Modifier
d) All of the mentioned

8. Which of these access specifiers can be used for a class so that it’s members can be accessed by a
different class in the different package?
a) Public
b) Protected
c) Private
d) No Modifier

9. Which of the following is incorrect statement about packages?


a) Package defines a namespace in which classes are stored.
b) A package can contain other package within it.
c) Java uses file system directories to store packages.
d) A package can be renamed without renaming the directory in which the classes are stored.

10. What will this code print?


int arr[] = new int [5];
System.out.print(arr);

a) 0
b) value stored in arr[0].
c) 00000
d) Some reference value

11. Which of these is an incorrect Statement?


a) It is necessary to use new operator to initialize an array.
b) Array can be initialized using comma separated expressions surrounded by curly braces.
c) Array can be initialized when they are declared.
d) None of the mentioned

12. What is the output of this program?


1. class array_output {
2. public static void main(String args[])
3. {
4. int array_variable [] = new int[10];
5. for (int i = 0; i < 10; ++i) {
6. array_variable[i] = i;
7. System.out.print(array_variable[i] + " ");
8. i++;
9. }
10. }
11. }
a) 0 2 4 6 8
b) 1 3 5 7 9
c) 0 1 2 3 4 5 6 7 8 9
d) 1 2 3 4 5 6 7 8 9 10

13. What is the output of this program?


1. class evaluate {
2. public static void main(String args[])
3. {
4. int arr[] = new int[] {0 , 1, 2, 3, 4, 5, 6, 7, 8, 9};
5. int n = 6;
6. n = arr[arr[n] / 2];
7. System.out.println(arr[n] / 2);
8. }
9. }

a) 3
b) 0
c) 6
d) 1

14. What is the output of this program?


1. class array_output {
2. public static void main(String args[])
3. {
4. char array_variable [] = new char[10];
5. for (int i = 0; i < 10; ++i) {
6. array_variable[i] = 'i';
7. System.out.print(array_variable[i] + "");
8. }
9. }
10. }

a) 1 2 3 4 5 6 7 8 9 10
b) 0 1 2 3 4 5 6 7 8 9 10
c) i j k l m n o p q r
d) i i i i i i i i i i

15. What is not true in case of Static variables?


a) static keyword can be applied with variable, method and to class with restriction.
b) static variables are associated with class instead of object.
c) static variables in java keeps different values for every single object.

16. A final method


a. cannot be overridden by any subclasses.
b. Can be overridden by any subclasses.
c. Revokes the memory when called
d. Is called by JVM.

17. 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. }
16. }
a) False
b)True
c)0
d)1

18. Which of these statements 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 inheritance.
c) main() method must be made public.
d) All object of a class are allotted memory for the methods defined in the class.

19. 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

20. What is the stored in the variable obj of type Box in following lines of code?
Box obj;
a) Memory address of allocated memory of object.
b) NULL
c) Any arbitrary pointer
d) Garbage

21. Which of these keywords is used to make a class?


a) class
b) struct
c) int
d) None of the mentioned

22. 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;

23. Which of these operators is used to allocate memory for an object?


a) malloc
b) alloc
c) new
d) give

24. Which of these statements 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.

25. 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.

26. How many string objects have been created


String x = new String("xyz");

String y = "abc";

x = x + y;

a) 2
b) 3
c) 1

27. The Java compiler produces


(a) Java bytecode

(b) machine language

(c) assembly language

(d) an iterated list

(e) a html file

28. Which of these selection statements test only for equality?


a) if
b) switch
c) if & switch
d) None of the mentioned

29. 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.

30. 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
31. Which of the following statements is not true:
a) The direct superclass is the superclass from which the subclass explicitly inherits

b) An indirect superclass is any class above the direct superclass in the class hierarchy

c) InJava,the class hierarchy begins with class Object

d) Signle inheritance means each class is derived from at least one direct super class

32. Which of following is an example of is-a-relationship


a) Car-vehicle

b) Car-steering wheel

c) Car-truck
d) Car-showroom

33. All public and protected superclass members retain their original access modifier when they
become members of the subclass. What about superclass’s private members:
a) They are not directly accessible outside the class itself.

b) They’re hidden in its subclasses and can be accessed only through the public or protected
methods inherited from the superclass.

c) They can be accessed from the subclass by preceding the superclass private member name
with keyword super and a dot (.) separator

d) Only a and b are true

e) Only a and c are true

Consider the following code for next three questions

1. class SuperClass{
2. int num;
3. SuperClass(){
4. num=0;
5. System.out.println("Super class's no argument constructor");
6. }
7. SuperClass(int n){
8. num=n;
9. System.out.println("Super class's parameterized constructor");
10. }
11. }

12. class SubClass extends SuperClass{


13. SubClass(){
14. super.SuperClass();
15. }
16. }

34. What happens if we create an instance of SubClass in main() method as SubClass objSub=new
SubClass();
a) There will be an output “Super class's no argument constructor”

b) There will be an output “Super class's parameterized constructor”

c) Line 16 will throw an exception

d) Compiler will give an error message for line 16

35. It is claimed that line 16 has an error because:


a) Default Constructor is private in superclass

b) Default constructor is not private in superclass but it can not be called explicitly from
constructor of its base class

c) Constructors are not inherited, so class SubClass does not inherit class SuperClass’s
constructor. However, SuperClass’s constructors are still available to SubClasses so there will
be no error

d) This is not a valid statement to call constructor of a superclass explicity, it should be called
with just writing super keyword and parethesis: super()

36. If we don’t write any constructor for SubClass:


a) There will be an error message

b) Java implicitly calls the SubClass’s default constructor and then SuperClass’s default or no-
argument constructor.

c) Java will implicitly call the default constructor of SubClass and the first task of that subclass
constructor will be to call SuperClass’s constructor

d) No object will be created.

37. Declaring overridden methods with the @Override annotation:


a) Ensures at compilation time that you defined their signatures correctly

b) Is necessary to override a superclass method in base class, compiler will give an error
message otherwise.

c) Ensures at run time that you defined their signatures correctly

d) It is your choice; if you use the annotation, the method is overridden, if you don’t , method
with same signature is made in the subclass class but is not overridden.

38. What will happen when we call myMthod() through an object of SubClass in the following code

1. class SuperClass{
2. void myMethod(){
3. System.out.print("superclass version of myMethod");
4. }
5. }///
6. class SubClass extends SuperClass{
7. void myMethod(){
8. myMethod();
9. System.out.println(" subclass version of
myMethod");
10. }
11. }

a) Compilation Error because @Override is not used while overriding myMehtod() in SubClass

b) Compilation Error because two methods with same signatures can not exist in a Superclass
and its subclass

c) It will cause the SubClass method myMwthod() to call itself, potentially creating an error
called infinite recursion

d) It will print:” superclass version of myMethod subclass version of myMethod”

39. Which of the following statements is false?


a) A class can directly inherit from class Object.
b) It's often much more efficient to create a class by inheriting from a similar class than to
create the class by writing every line of code the new class requires.
c) A class's instance variables are normally declared private to enforce good software
engineering.
d) If the class you're inheriting from declares instance variables as private, the inherited class
can access those instance variables directly.

40. Say that class Mammal has a child class Whale and another child class Ape. Class Ape has a child
class Monkey. Examine the following
Mammal mml;

Whale whl = new Whale();

Ape ape = new Ape();

Monkey mnk = new Monkey();

Which one of the following will cause a compiler error?

a) mml = whl;

b) mml=ape;

c) mnk=null;

d) mnk=whl;

41. What will happen if we write the following line of code in the above question scenario

boolean b= ape instanceof Animal;


System.out.println(b );

a) It will cause a compiler error

b) It will print true

c) It will print false

d) It will throw ClassCastException at runtime

42. For which of the following would polymorphism not provide a clean solution?
a) A billing program where there is a variety of client types that are billed with different fee
structures.
b) A maintenance log program where data for a variety of types of machines is collected and
maintenance schedules are produced for each machine based on the data collected.
c) A program to compute a 5% savings account interest for a variety of clients.
d) An IRS program that maintains information on a variety of taxpayers and determines who to
audit based on criteria for classes of taxpayers.

43. Multiple inheritance is not supported in case of class. But it is supported in case of interface
because:
a) Class may or may not be abstract while interface gives 100% abstraction.

b) There is no ambiguity as implementation is provided by the implementation class.

c) Multiple inheritance is not supported in case of interface either.

d) Methods of an interface cannot be overridden in the class that implements that interface.

44. Which of following is true in case of interface


a) An interface extends another interface

b) A class extends an interface

c) An interface implements another interface

d) A class extends more than one interfaces

45. Consider a super class namely SuperClass and a sub class namely SubClass , what does the
following lines of code mean:
SuperClass obS=new SuperClass();
SubClass obSub=new SubClass();
obSub=(SubClass)obS;

a) we are performing downcasting by typecasting.

b) ClassCastException is thrown at runtime


c) we are performing downcasting by typecasting but it will give an error without using
instanceof operator

d) Both a and b is true

46. What is true about The java instanceof operator :

a) It is used to test whether the object is an instance of the specified type (class or subclass or
interface).

b) Compares the instance with type.

c) Returns either true or false.

d) All of above are true

47. Which of following is right about abstract class

a) It cannot be extended.

b) The abstract class can be used to provide some implementation of the interface.

c) It must have at least one abstract method.

d) All of above

48. Which of following is/are true


i. If you make any method as final, you cannot override it.

ii. If you make any class as final, you cannot extend it.

iii. If you make any method as final, it is static by default.

iv. If you make any class as final, you cannot instantiate it.

a) Only i and ii

b) Only ii and iii

c) Only iii and iv

d) None is true

49. What features an abstract class belong but not an interface?


a) Abstract class can have discrete methods while interface cannot; not even static methods

b) Abstract class can have constructors while an interface cannot

c) Abstract class can instantiate while interface cannot

d) Abstract can have public members while an interface cannot


50. If the catch-or-declare requirement for a checked exception is not satisfied ________.

a) The compiler will issue an error message indicating that the exception must be caught.

b) The compiler will issue an error message indicating that the exception must be caught or
declared.

c) A stack trace will be displayed indicating the exception that has occurred and where it
occurred.

d) A stack trace will be displayed, along with a message indicating that the exception must be
caught.

51. Which of following is/are true for checked exceptions:


a) The classes that extend Throwable class except RuntimeException and Error are known as
checked exceptions

b) The classes that extend Throwable class are known as checked exceptions

c) Examples are ArithmeticException, NullPointerException, ArrayIndexOutOfBoundsException

d) Checked exceptions are checked at run-time.

52. Which of following is/are true for Unchecked Exceptions:


a) These extend RuntimeException

b) Examples are ArithmeticException, NullPointerException, ArrayIndexOutOfBoundsException

c) Tthese are not checked at compile-time rather they are checked at runtime.

d) All are true

e) Only b and c are true

53. If we divide any number by zero, there occurs a/ an


a) ArithmeticException.

b) DivideByZeroException

c) InfinitException

d) It causes a compile time error not an Exception

54. If we have null value in any variable, performing any operation by the variable occurs a
NullPointerException. Which of following options explains it while keeping in view this line of
code: String s=null;
a) System.out.println(s);
b) System.out.println(s.length( ));

c) String s1=s;

d) String s2=” ” + s;

55. What is the scenario where ArrayIndexOutOfBoundsException occurs while considering the
following line of code:
int a[]=new int[5];

a) a[4]=5;

b) int b=a[5];

c) a[4]= a[4]+1;

d) all of above

56. Which of followings is Not an Exception Handling keywords


a) try

b) catch

c) finalize

d) throws

57. Which of following is true


a) An exception is first thrown from the top of the stack and if it is not caught, it drops down
the call stack to the previous method, If not caught there, the exception again drops down to
the previous method, and so on until they are caught or until they reach the very bottom of
the call stack.

b) An exception is first thrown from the bottom of the stack and if it is not caught, it goes up
the call stack to the next method, If not caught there, the exception again goes up to the
next method, and so on until they are caught or until they reach the very top of the call
stack.
c) Stack is not concerned with exception propagation
d) Method activation stack converts to heap for Exception handling

58. Which of these options will result if we run this code:


int arr[]=new int[4];
int a=arr[4]/0;
a) It will throw ArrayIndexOutOfBoundsException at run time
b) It will throw ArithmeticException at run time
c) It will throw ArrayIndexOutOfBoundsException and ArithmeticException at run time
d) It will cause a compiler error that array elements are not initialized before using

59. Which of following is Not correct about throws keyword in java


a) You can declare multiple exceptions with a method signature

b) Throws is used with the method signature

c) Checked exception can be propagated with throws

d) Java throws keyword is used to explicitly throw an exception.

60. ---- package contains all the classes required for input and output operations.
a) Java.iostream
b) Java.io
c) Java.file
d) Java.filestream

61. Java application uses a/an ----to write data to a destination, it may be a file, an array, peripheral
device or socket.
a) output stream
b) input stream
c) err stream
d) all of above

62. Which of following is not true

a) In java, 3 streams are created for us automatically. All these streams are attached with file.

b) These streams are System.out System.in System.err

c) Option b is right but all these streams are attached with console.

d) None of them

63. Consider following code

1. import java.io.FileOutputStream;

2. public class FileOutputStreamExample {

3. public static void main(String args[]){


4. try{

5. FileOutputStream fout=new FileOutputStream("D:\\testout.txt");

6. String s="File handling is an interesting topic.";

7. byte b[]=s.getBytes();//converting string into byte array

8. fout.write(b);

9. fout.close();

10. System.out.println("success...");

11. }catch(Exception e){System.out.println(e);}

12. }

13. }

What the above code does:

a) The content of a java file FileOutputStreamExample.java is set with the data from File
testout.txt

b) The content of a text file testout.txt is set with the data File handling is an interesting topic.

c) The content of a text file testout.txt is set with whatever is written by user on input console

d) All three possibilities are there depending upon the platform on which JVM runs

e) The code will not compile successfully as it has a compiler error at line 5.

64. What is the right code to create a BufferedReader object for a file D:/Sample.txt

a) reader = newBufferedReader(newReader("C:\\sample.txt"));

b) reader = newBufferedReader(newFileReader("C:\\sample.txt"));

c) reader = newReader (newBufferedReader ("C:\\sample.txt"));

d) reader = newFileReader (newBufferedReader ("C:\\sample.txt"));

65. GUIs are event driven. It means:

a) When the user interacts with a GUI source code, the interaction—known as an event—drives
the program to perform a task.

b) When the user interacts with a GUI component, the interaction—known as an event—drives
the program to perform a task.
c) When the user interacts with a GUI component, the interaction—known as an eventHandler
—drives the program to perform a task.

d) When the user interacts with a GUI JButton, the interaction—known as an event—drives the
program to perform a task.

66. The code that performs a task in response to an event is called a/an ----

a) Event handler

b) Event driven

c) Event Handling

d) Event

67. event handlers are often implemented as private inner classes because

a) they tend to be specific to the application in which they are defined

b) no class can be private because a private class cannot be accessed even if it is defined as inner
class of the class trying to access.

c) Private is the only option to declare an inner class

d) Private classes save space in memory

68. To handle ActionEvents:

a) a class must extend class ActionListener and declare method actionPerformed.

b) a class must extend class ActionPerformed and declare method ActionListener.

c) a class must implement interface ActionListener and declare method actionPerformed.

d) a class must implement interface ActionPerformed and declare method ActionListener.

69. JCheckBox method ----- returns true if a JCheckBox is selected.

a) isSelected

b) isChecked

c) Selected

d) Checked

70. Which of following is true about anonymous inner class:

a) An inner class that is declared without a name and typically appears inside a method
declaration.
b) As with other inner classes, an anonymous inner class can access its top-level class’s
members.

c) An anonymous inner class has limited access to the local variables of the method in which it’s
declared.

d) Since an anonymous inner class has no name, one object of the anonymous inner class must
be created at the point where the class is declared.

e) All are true

71. Which of following is a valid statement to create a button with string label Click on it

a) JButton b = new JButton(Click);

b) JButton b = new JButton("Click");

c) JButton b = JButton .setTitle(Click);

d) JButton b = JButton .setTitle(“Click”);

72. How the JButton object b created previously can be added to a JFrame object Jf

a) b=add(Jf);

b) Jf=add(b);

c) Jf.add(b);

d) b.add(Jf);

73. Which of following is not a keyListener method


a) keyPressed(KeyEvent e)
b) keyReleased(KeyEvent e)
c) keyTyped(KeyEvent e)
d) keyClicked(KeyEvent e)

74. Which of following are methods of MouseListener


a) mouseReleased(MouseEvent e)
b) mouseEntered(MouseEvent e)
c) mouseExited(MouseEvent e)
d) mouseClicked(MouseEvent e)
e) All of above

75. --- A drop-down list of items from which the user can make a selection
a) JButton
b) JCheckBox
c) JComboBox
d) JList

1. String firstNumber=JOptionPane.showInputDialog( "Enter first


integer");
2. String secondNumber = JOptionPane.showInputDialog( "Enter second
integer" );
3. int number1 = Integer.parseInt( firstNumber );
4. int number2 = Integer.parseInt( secondNumber );
5. int sum = number1 + number2;
6. JOptionPane.showMessageDialog( null, "The sum is " + sum, "Sum of
Two Integers", JOptionPane.PLAIN_MESSAGE );

Consider the above lines of code and answer following questions

76. What lines 1&2 intend to do


a. Printing two MessageDialog to display Enter first integer and Enter second integer
respectively.
b. Both lines are taking integer values that cannot be converted to String so it will cause an
error
c. Obtain user input from JOptionPane input dialogs
d. Set the firstNumber with value “Enter first integer” and secondNumber with the value “Enter
second integer” successfully.

77. What line 6 intends to do


a. display result in a JOptionPane message dialog
b. It will show a JOptionPane message dialog but nothing else because we are passing null as
first argument
c. display result in a JOptionPane input dialog
d. It will show a JOptionPane input dialog but nothing else because we are passing null as first
argument

78. Which of following are valid statements to create a String object

a. String message = "Welcome to Java“;

b. String message = new String("Welcome to Java“);


c. String message = {"Welcome to Java“};

d. a & b

e. All a, b & c

79. What does the default StringBuilder constructor do?

a. Constructs an empty StringBuilder with capacity 32

b. Constructs an empty StringBuilder with capacity 16

c. Constructs an empty StringBuilder with capacity 0

d. All of above

80. Which of following is true:


a) Every object can access a reference to itself with the keyword new.
b) Overloaded methods enable objects of a class to be initialized in different ways.

c) The compiler differentiates signatures by the number of parameters, the types of the
parameters and the order of the parameter types in each signature.
To overload constructors, simply provide multiple constructor declarations with different signatures and
return type.
Question.1 [1*10 = 10 Marks]
For the multiple choice questions given below, encircle the most suitable answer. Multiple selection and
cutting will lead to 0 marks.
1. An array object, ArrayOne, is created as:

float[][] ArrayOne;
ArrayOne = new float[20][10];
Suppose ArrayOne is passed as an argument to a method in which the corresponding parameter
is named someArray. What should the declaration of someArray look like in the parameter list
of the method?
a) float [][] someArray

b) float someArray[]

c) float [] someArray[20]

d) float someArray[20][10]

2. Which of the following is correct about object?


a) An object is like the blueprint, and we can have many instances of it, which are called
classes
b) A class is like the blueprint, and we can have many instances of it called, which are
called objects
c) There is no difference between a class and an object
d) The object keyword indicates we’re defining an object and the class keyword
indicates we’re defining a class
3. If a variable is declared static in a particular class, what does that mean?
a) It cannot be changed
b) It can only be changed by the instances of the class in which it is declared
c) There is only 1 copy of it that all instances of the class can access
d) Each instance of the class has its own copy of the variable
4. What does just this piece of code do: Song[] music;
a) Creates a new array of references to Song objects, called music
b) Creates a new array of references to music objects, called Song
c) Declares a variable called music whose type is an array of Song objects, but does not
create an actual array
d) Declares a variable called music whose type is null

Next 5-9 Questions refer to the following code.


1. public class Desk
2. {
3. private int length;
4. private int width;
5. private int height;
6.
7. public Desk(int length, int width, int h)
8. {
9. length = length;
10. this.width = width;
11. height = this.h;
12. }
13.
14. public int getLength() {
15. return this.length;
16. }
17.}

5. What are the fields defined in this class?


a. The three integers passed as parameters in the constructor, defined in line 7
b. The three integers declared in lines 3-5
c. There are none
d. Only the ones where the this keyword is used in front of them

6. What is line 9 doing?


a. Assigning the value of the parameter length to the field length
b. Assigning the value of the field length to the parameter length
c. Assigning the value of the parameter length to itself, i.e. doing nothing useful
d. Assigning the value of the field length to itself, i.e. doing nothing useful Intro to
Programming in Java 600.107 Johns Hopkins University 10
7. Line 10 is attempting to assign the value of width, passed into the constructor, to the variable
width in the Desk class. It was claimed that the this keyword is not needed. Which of the
following is true?
a) It is needed, but the left and right hand sides of the assignment should be swapped, so
that the equation is: width = this.width;
b) It is needed, because we need to differentiate between the width parameter and the
width field (use this to refer to the field one), and the assignment is correct as written.
c) It is needed, because we need to differentiate between the width parameter and the
width field (use this to refer to the parameter one), and the assignment is correct as
written.
d) It is not needed, and should be rewritten as: width = width;

8. Line 11 is attempting to assign the value of h to the variable height. It was claimed that there
is an error in line 11. Which of the following is true?
a) The error is there’s no field h in the Desk class, so can’t write this.h. It should be
corrected to: this.height = h;
b) The error is the same as specified in option A, but it should be corrected to the
equation: h = height;
c) The error is simply that the right and left hand sides of the assignment are switched. It
should be corrected to: this.h = height;
d) There is no error.

9. Assume that any compilation (i.e. compile time) errors in the above code are corrected. If an
instance of this class is made with the instantiation:
Desk d = new Desk(1,2,3); What is the return value of calling
d.getLength()? The default value of integer field variables is 0.
a) 0
b) 1
c) 2
d) 3

10. You have two methods named calc in the same class that both return an integer, but one
takes 1 double and the other takes 2 doubles. We say method calc is:
a) Overridden
b) Overloaded
c) Instantiated
d) Invoked

11. How many objects of a given class can be created in a program?

a)One per defined class

b) One per constructor definition

c)As many as the program needs

d) One per main() method


12. What type of relationship exists between someMethod in classes A and someMethod in class B?

class A
{
public void someMethod(String x)
{
System.out.println( "from class A " + x );
}
}
-----------------------
class B extends A
{
public void someMethod( String x )
{
System.out.println( "from class B:" + x );
}
}

a) method overriding

b) method overloading

c) both method overriding and method overloading

d) neither method overriding nor method overloading

13. 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.

14. Which of the following is true about an abstract method inherited into a class C?

a)It must be defined in C before C can be instantiated

b) It must be defined in C or C should be abstract

c)If it is defined in C and it is only method in C, then C would be a non-abstract


class

d) All of above is true


15. What are valid arguments to the instanceof operator?

a)any primitive type

b) a class object and a class type

c)boolean type only

d) class types only

16. What is garbage collection in the context of Java?

a)The operating system periodically deletes all of the java files available on the
system.

b) Any package imported in a program and not used is automatically


deleted.

c)When all references to an object are gone, the memory used by the object is
automatically reclaimed.

d) The JVM checks the output of any Java program and deletes anything
that doesn't make sense.

17. Providing access to an object only through its member functions, while keeping the details private
is called

a)Information hiding

b) Encapsulation

c)Inheritance

d) Modularity

18. In a student grading system, objects from different classes communicate with each other. These
communications are known as _____.

a)Inheritance

b) Message Passing

c)Polymorphism

d) SMS and MMS

19. Suppose the class Chair extends the class Furniture. Both classes are non-abstract. Which of the
following assignments are legal?
I. Chair c = new Chair();
II. Furniture f = new Furniture();
III. Furniture f = new Chair();
IV. Object o = new Chair();

a) I and II only
b) II and III only

c) All are legal statements

d) I, II and III only

20. Which of these statements about constructors is false?


A constructor has no return type
a) Its name must be the same as the class in which it is defined
b) There must be exactly one constructor defined for a class
c) Constructors are almost always declared as public
d) They can appear anywhere in the class where it is legal to declare a
method

21.When does Java know an object is no longer needed? And what happens
to an unneeded object's storage?
a) The programmer tells Java when an object is no longer needed by
calling dispose() on it; the object's memory is released back to the
memory pool.
b) If there are no references to the object, Java knows the object is no
longer needed and automatically returns its memory to the memory
pool.
c) If there are no references to the object, Java marks it as no longer
needed; the memory stays in use until the programmer explicitly
returns it to the memory pool.
d) Objects, once constructed, stay active until the program terminates,
so thought he programmer may know an object is it no longer
needed, Java does not know this; objects' memory is returned to the
memory pool when the program terminates.
e) Objects, once constructed, stay active until the method in which
they were constructed terminates, so though the programmer may
know an object is no longer needed, Java does not know this;
objects' memory is returned to the memory pool when the method
terminates.

22.Which of the following Java statements set even to true if n is even, and
to false if n is odd? (n is an integer.) Assume n >= 0. (Even numbers are
those integers which, when divided by 2, have a remainder of 0.)
I. boolean even = (n/2.0 == (double)(n/2));
II. boolean even = (n % 2 == 0);
III. boolean even = (n div 2 == 0);
IV. boolean even = (n % 2 == n/2);

a) I only D. III and IV only


b) I and II only E. I, II and IV only
c) II and III only

23. Which of the following expressions is equivalent to the boolean


expression
!(A < 5 && B != C)

a) A > 5 || B != C
b) A >= 5 && B == C
c) !(A < 5) || (B != C)
d) A >= 5 || B == C
e) A < 5 && B == C
Solution

1 2 3 4 5 6 7 8 9 10
D D C D B D B B C
B

11 12 13 14 15 16 17 18 19 20
B B D B A D D B B
B

21 22 23 24 25 26 27 28 29 30
D A D C B C B C C
D

31 32 33 34 35 36 37 38 39 40
A C C A D C C D B
A

1 2 3 4 5 6 7 8 9 10 Marks
D B A D B C C D E D
11 12 13 14 15 16 17 18 19 20
C B C D D D D B B C
21 22 23 24 25 26 27 28 29 30
A B B A B A C B C B

1 2 3 4 5 6 7 8 9 10

D D D B C C D A D D

11 12 13 14 15 16 17 18 19 20

A A D D C A B D B B

21 22 23 24 25 26 27 28 29 30
a A c a a a a b b d

31 32 33 34 35 36 37 38 39 40

d A D d d c a c d d

41 42 43 44 45 46 47 48 49 50

b C B a d d b a b b

51 52 53 54 55 56 57 58 59 60

a D A b b c a a d b

61 62 63 64 65 66 67 68 69 70

a C B b b a a c a e

71 72 73 74 75 76 77 78 79 80

b C D e c c a d b c

S-ar putea să vă placă și