0% found this document useful (0 votes)
2K views

Diagnostic Test - Programming Java

Object-oriented programming provides clear modular structure, makes code easy to maintain and modify, and provides a good framework for code libraries. The output of the sample code is 4. HAS-A relationships are based on usage, not inheritance. The code can be corrected by creating an object of the SuperHotel subclass and calling book(2) from it. Overriding a method that throws an exception requires either removing the exception or adding it to the overriding method signature.

Uploaded by

Fernan Enad
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2K views

Diagnostic Test - Programming Java

Object-oriented programming provides clear modular structure, makes code easy to maintain and modify, and provides a good framework for code libraries. The output of the sample code is 4. HAS-A relationships are based on usage, not inheritance. The code can be corrected by creating an object of the SuperHotel subclass and calling book(2) from it. Overriding a method that throws an exception requires either removing the exception or adding it to the overriding method signature.

Uploaded by

Fernan Enad
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

1.

The benefits of the Object Orientation are: (choose two)


A. Inheritance
B. Flexibility
C. Maintainability
D. Polymorphism
Object-Oriented Programming has the following advantages over conventional approaches:
OOP provides a clear modular structure for programs which makes it good for defining abstract datatypes where
implementation details are hidden and the unit has a clearly defined interface.
OOP makes it easy to maintain and modify existing code as new objects can be created with small differences to
existing ones.
OOP provides a good framework for code libraries where supplied software components can be easily adapted and
modified by the programmer. This is particularly useful for developing graphical user interfaces.
2. Given below the sample code:
class Hotel {
public int bookings=2;
public void book() {
bookings++;
}
}
public class SuperHotel extends Hotel {
public void book() {
bookings--;
}
public void book(int size) {
book();
super.book();
bookings += size;
}
public static void main(String args[]) {
SuperHotel Shotel = new SuperHotel();
Shotel.book(2);
System.out.print(Shotel.bookings);
}
}
Find the output of the following code:
A. Compile error
B. 2
C. 4
D. No Output
3. HAS-A relationships are based on inheritance, rather than usage.
A. True
B. False
4. Given below the sample code:
1 class Hotel {
2 public int bookings;
3 public void book() {
4 bookings++;
5 }
6 }
7 public class SuperHotel extends Hotel {
8 public void book() {
9 bookings--;
10 }
11 public void book(int size) {

12 book();
13 super.book();
14 bookings += size;
15 }
16 public static void main(String args[]) {
17 Hotel hotel = new Hotel();
18 hotel.book(2);
19 System.out.print(hotel.bookings);
20 }}
How can we correct the above code ? (choose all that apply)
A. By adding argument "int size" to the method book at line number 3.
B. By removing argument '2' at line number 18.
C. By creating object of "SuperHotel" subclass at line 17 & calling book(2) from it at line 18
D. No correction needed.
5. Given below the sample code:
import java.io.IOException;
class Example7 {
public float Twin(float x, float y)
throws IOException {
return 0;
}
}
class SubExample7 extends Example7 {
float Twin(float x, float y) {
return 0;
}
}
How can we correct above code? (choose two)
A. No need for correction.
B. By changing the method's argument name.
C. By removing overridden method's access specifier (i.e public).
D. By adding access specifier 'public' to the overriding method.
6. In Java, the actual method executed is determined by the type of the object and not the type of the reference.
A. True
B. False
7. The methods in class java.lang.Object are (choose four).
A. Clone
B. Notify
C. Concat
D. Wait
E. Equals
F. Compare
Object class is the superclass of all Java classes. All Java classes inherited from this class. This makes it possible
that we can have methods that are available in all Java classes.
equals();
getClass();
hashCode();
notify();
notifyAll();
toString();
wait();
clone();
finalize();
8. Array or collection of superclass references can be used to access a mixture of superclass and subclass objects.
A. True
B. False

9. Given the following sample code:


public class Example5{
public float Twin(float a, float b) {... }
public float Twin(float a1, float b1) { ...}
}
How can we correct the above code? (choose two)
A. By placing overriding method into subclass.
B. By changing the name of the class.
C. By replacing overloading with overriding.
D. By changing the name of the arguments.
10. A class can inherit instance variables and methods from a more abstract superclass.
A. True
B. False
11. At run-time, a Java program is nothing more than objects talking to ___________.
A. Other objects
B. Other methods
C. Other classes
D. Other binders
12. If you do not have access to the source code for a class, but you want to change the way a method of that class
works, then could you use subclassing to do that that is to extend the bad class and override the method with
your own better code?
A. True
B. False
13. The relation between Car and Owner or BankAccount and Customer is example for
A. Aggregation
B. Composition
C. Association
D. None
14. Aggregation is a special form of association.
A. True
B. False
Association is a relationship between two objects. In other words, association defines the multiplicity between
objects.
Aggregation is a special case of association. A directional association between objects. When an object has-a
another object, then you have got an aggregation between them. Direction between them specified which object
contains the other object.
15. Subclassing polymorphism is sometimes called true polymorphism.
A. True
B. False
16. A method defined in a superclass is redefined in a subclass with an identical method signature is
called___________.
A. Method overloading
B. Method overriding
C. Dynamic binding
D. Late binding
17. Consider the below code and choose the correct output.
public class Main {
public int a;
public long b;
public void test(long b)
{
System.out.println("Long b");
}
public void test(int a)
{
System.out.println("Int a");

}
public static void main(String[] args) {
Main e=new Main();
e.test(9*1000000000);

}
}
A. Int a
B. Long b
C. Long a
D. Error
18. An interface cannot have an inner class.
A. True
B. False
19. Polymorphism is one interface with __________.
A. Single method
B. Multiple methods
C. Multiple record
D. Single record
20. Method overloading is done during _______.
A. Runtime
B. Dynamic binding
C. Program compilation
D. Late binding
21. Interfaces are fast as it requires extra indirection to find corresponding method in the actual class.
A. True
B. False
22. Ad hoc polymorphism is ____________.
A. Method Overloading
B. Method Overriding
C. Subclassing polymorphism
D. Dynamic binding
23. The inheriting class cannot override the definition of existing methods by providing its own implementation.
A. True
B. False
24. The ability to make changes in your implementation code without breaking the code of others who use your
code is a key benefit of _______________.
A. Extensibility
B. Polymorphism
C. Inheritance
D. Encapsulation
25. Every class in Java is a subclass of class _____________.
A. Inheritance
B. Object
C. Exception
D. ArrayList
26. Consider the code below and choose the correct option.
class GameShape {
public void displayShape() {
System.out.println("displaying shape");
}

// more code
}
class PlayerPiece extends GameShape {
public void movePiece() {
System.out.println("moving game piece");
}
// more code
}
public class TestShapes {
public static void main (String[] args) {
PlayerPiece shape = new PlayerPiece();
shape.displayShape();
shape.movePiece();
}
}
A. PlayerPiece class inherits the generic movePiece() method
B. PlayerPiece class inherits the generic displayShape() method
C. GameShape class inherits the generic displayShape() method
D. GameShape class inherits the generic movePiece() method
27. The two most common reasons to use inheritance are (choose 2)
A. To promote code reuse
B. To use abstraction
C. To use interface
D. To use polymorphism
28. A class is not an object. But it is used to construct objects.
A. True
B. False
29. Examples of class are (choose 3)
A. White
B. Length
C. Classroom
D. Car
E. Person
30. In OO, the concept of IS-A is based on
A. Class inheritance
B. Interface implementation.
C. Encapsulation
D. None

You might also like