1.
Which of the following is true about inheritance in Java?
1) Private methods are final.
2) Protected members are accessible within a package and
inherited classes outside the package.
3) Protected methods are final.
4) We cannot override private methods.
Correct Answer
A. 1,2 and 4
Explanation
Private methods in Java are not final. Final methods cannot be overridden, but private methods cannot be accessed or inherited by any subclass. Protected members are accessible within a package and to inherited classes outside the package. We cannot override private methods because they are not accessible to the subclass. Therefore, the correct answer is 1, 2, and 4.
2.
By Default, members of a class can be accessed through its objects from
Correct Answer
B. Within the same packege
Explanation
Members of a class can be accessed through its objects from within the same package. This means that if a class is defined in a certain package, any other class within the same package can access its members using objects of that class. However, classes outside the package cannot access the members of the class directly. This is because access modifiers in Java, such as public, private, and protected, control the visibility and accessibility of class members.
3.
What will be output?
class T{
T()
{
super();
System.out.println("object");
}
public static void main(String []args){
T t = new T();
}
}
Correct Answer
B. Object
Explanation
The given code is a class named T with a constructor. In the constructor, the super() method is called, which is used to invoke the constructor of the superclass. In this case, since the class T does not have any explicit superclass, the super() method is not necessary. The code then prints "object" to the console. In the main method, an object of class T is created, which triggers the constructor and the output "object" is printed. Therefore, the correct answer is "object".
4.
What will be output?
class A{
A(){System.out.println("class A");}
}
class B extends A{
B(){System.out.println("class B");}
}
class C extends B{
C(){System.out.println("class C");}
}
class Main
{
public static void main(String args[]){
B obj=new C();
}
}
Correct Answer
C. Class A class B class C
Explanation
The output will be "class A class B class C". This is because the main method creates an object of class C and assigns it to a variable of type B. Since class C is a subclass of class B, this is allowed. When the object is created, the constructors of class A, class B, and class C are called in order. Therefore, the output is "class A class B class C".
5.
A sub class does not inherits
Correct Answer
C. All Constructors
Explanation
A subclass does not inherit all constructors from its superclass. Constructors are special methods used to initialize objects of a class. Each class can have its own constructors, and they are not inherited by subclasses. Subclasses can have their own constructors, but they cannot directly access or use the constructors of their superclass. Therefore, the correct answer is "All Constructors".
6.
In the following class, method declaration is?
class test
{ void show(int …a) {}
void show(int[] a){}
}
Correct Answer
B. Method duplicated
Explanation
The correct answer is "Method duplicated" because the class "test" has two methods with the same name "show", but with different parameters. This is known as method overloading, where multiple methods can have the same name but with different parameters. In this case, the two methods have different parameter types - one accepts a variable number of integers and the other accepts an array of integers.
7.
Super keyword in java is used to ?
Correct Answer
A. Refer to parent class object.
Explanation
The super keyword in Java is used to refer to the parent class object. It is often used to access the parent class's members (variables and methods) that have been hidden or overridden by the child class. This allows for the implementation of inheritance and facilitates code reuse and polymorphism.
8.
What will be output?
class Hotel {
public int bookings;
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 hotel = new SuperHotel();
hotel.book(2);
System.out.print(hotel.bookings);
}
}
Correct Answer
D. 2
Explanation
The output will be 2. In the main method, a new SuperHotel object is created. Then the book(int size) method is called with a size of 2. Inside the book(int size) method, the book() method is called, which decreases the bookings by 1. Then the super.book() method is called, which increases the bookings by 1. Finally, the size is added to the bookings. Since the initial value of bookings is 0, the final value will be 2.
9.
What will be output?
class Vehicle {
public void printSound() {
System.out.print("vehicle");
}
}
class Car extends Vehicle {
public void printSound() {
System.out.print("car");
}
}
class Bike extends Vehicle {
public void printSound() {
System.out.print("bike");
}
}
public class Test {
public static void main(String[] args) {
Vehicle v = new Car();
Bike b = (Bike) v;
v.printSound();
b.printSound();
}
}
Correct Answer
B. An exception is thrown at runtime.
Explanation
The code is trying to cast a Car object to a Bike object, which is not possible because Car is a subclass of Vehicle and Bike is also a subclass of Vehicle. This will result in a ClassCastException at runtime.
10.
What will be output?
class Num
{
Num(double x )
{
System.out.println( x ) ;
}
}
class Numbers extends Num
{
public static void main(String[] args)
{
Num num = new Num( 2 ) ;
}
}
Correct Answer
B. "2.0"
Explanation
The output will be "2.0" because the constructor in the Num class takes a double parameter, and when the object is created in the Numbers class, the value 2 is passed as an argument. Since 2 is an integer literal, it is automatically promoted to a double value of 2.0. Therefore, the output will be 2.0.
11.
In genereal the order of the three top level elements of the java source file are
Correct Answer
C. Package, Import, Class
Explanation
The correct order of the three top-level elements in a Java source file is Package, Import, Class. The package statement is used to define the package that the file belongs to. The import statements are used to import external classes or packages that are needed in the file. Finally, the class declaration is used to define the main class in the file.
12.
Object-oriented inheritance models the
Correct Answer
A. "is a ” relationship
Explanation
Object-oriented inheritance models the "is a" relationship. Inheritance allows a class to inherit the properties and behaviors of another class, creating a hierarchy where a subclass is a specialized version of its superclass. This relationship implies that the subclass shares common characteristics with the superclass but may also have additional attributes or behaviors specific to itself. By using inheritance, code reuse and abstraction can be achieved, making it easier to organize and maintain complex software systems.
13.
The concept of multiple inheritances 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.
Correct Answer
C. II and III
Explanation
The correct answer is II and III. In Java, multiple inheritances can be achieved by extending one class and implementing one or more interfaces (II) or by implementing two or more interfaces (III). Java does not support extending two or more classes (I) due to the issue of diamond inheritance problem. This problem occurs when a class inherits from two classes that have a common superclass, causing ambiguity. So, the correct way to achieve multiple inheritances in Java is through the use of interfaces.
14.
What is the error in the following class definitions?
abstract class Math
{
abstract sum (int x, int y) { }
}
Correct Answer
C. Method is not defined properly
Explanation
The error in the given class definitions is that the method "sum" is not defined properly. In an abstract class, abstract methods should have a return type specified. In this case, the return type of the "sum" method is missing, which is incorrect.
15.
Which of these field declarations are legal within the body of an interface?
Correct Answer
B. Public static int answer=42
Explanation
The correct answer is "public static int answer=42". In an interface, all fields are implicitly public, static, and final. Therefore, the field declaration "public static int answer=42" is legal within the body of an interface. The other options are not legal because they either have incorrect access modifiers (private) or are missing the data type (final static answer =42) or are missing a semicolon (int answers).
16.
. Which of the three keywords are valid when preceeding the keyword int in the following code?
public interface JavaQuiz {
int variable = 1000;
}
Correct Answer
A. Public , static and final
Explanation
The three valid keywords that can precede the keyword int in the given code are public, static, and final. These keywords are used to define the accessibility, behavior, and immutability of the variable. The public keyword allows the variable to be accessed from anywhere, the static keyword makes the variable shared among all instances of the class, and the final keyword makes the variable unchangeable once it is assigned a value.
17.
Which of these can be used to fully abstract a class from its implementation?
Correct Answer
C. Interfaces
Explanation
Interfaces can be used to fully abstract a class from its implementation. This is because an interface defines a contract or a set of methods that a class must implement. By using interfaces, the implementation details of a class are hidden, allowing different classes to provide their own implementation of the interface methods. This promotes loose coupling and allows for easier maintenance and flexibility in the code.
18.
What is the output of this program?
interface calculate {
void cal(int item);
}
class display implements calculate {
int x;
public void cal(int item) {
x = item * item;
}
}
class interfaces {
public static void main(String args[]) {
display arr = new display();
arr.x = 0;
arr.cal(2);
System.out.print(arr.x);
}
}
Correct Answer
C. 4
Explanation
The output of this program is 4.
In the main method, an object of the display class is created and assigned to the variable 'arr'. The variable 'x' is then initialized to 0.
The cal method is called on the 'arr' object with the argument 2. Inside the cal method, the value of 'x' is calculated by multiplying the 'item' parameter with itself (2 * 2 = 4).
Finally, the value of 'x' (which is 4) is printed using System.out.print.
19.
Which of these access specifiers can be used for an interface?
Correct Answer
A. Public
Explanation
The access specifier "Public" can be used for an interface. This means that the interface can be accessed by any class or code in the program. "Private" and "Protected" access specifiers cannot be used for an interface. "Private" restricts access to only within the same class, while "Protected" allows access within the same class and its subclasses. Therefore, the correct answer is "Public".
20.
Given the following piece of code:
public interface Guard{
void doYourJob();
}
abstract public class Dog implements Guard{ }
which of the following statements is correct?
Correct Answer
D. This code will compile without any errors.
Explanation
The code will compile without any errors because the class Dog is declared as abstract and it inherits the method doYourJob() from the interface Guard. Since it is an abstract class, it is not required to implement the method.
21.
Which of the following is FALSE about abstract classes in Java
Correct Answer
D. A class can inherit from multiple abstract classes
Explanation
In Java, a class can only inherit from one superclass, whether it is abstract or not. Multiple inheritance is not allowed in Java. Therefore, the statement that "A class can inherit from multiple abstract classes" is false.
22.
Which of the following is true about interfaces in java.
1) An interface can contain following type of members.
....public, static, final fields (i.e., constants)
....default and static methods with bodies
2) An instance of interface can be created.
3) A class can implement multiple interfaces.
4) Many classes can implement the same interface.
Correct Answer
A. 1, 3 and 4
Explanation
The correct answer is 1, 3 and 4.
1) An interface in Java can contain public, static, and final fields (constants), as well as default and static methods with bodies. This allows interfaces to provide constants and default implementations for methods.
3) A class in Java can implement multiple interfaces. This allows a class to inherit and implement multiple sets of behaviors from different interfaces.
4) Many classes in Java can implement the same interface. This allows for polymorphism and allows different classes to be used interchangeably when they implement the same interface.
23.
Predict the output of the following program.
abstract class demo
{
public int a;
demo()
{
a = 10;
}
abstract public void set();
abstract final public void get();
}
class Test extends demo
{
public void set(int a)
{
this.a = a;
}
final public void get()
{
System.out.println("a = " + a);
}
public static void main(String[] args)
{
Test obj = new Test();
obj.set(20);
obj.get();
}
}
Correct Answer
C. Compilation error
Explanation
The program will produce a compilation error. This is because the method "set(int a)" in the Test class does not match the abstract method "set()" in the demo class. The method in the Test class has a parameter, while the abstract method in the demo class does not have any parameters. Therefore, the program cannot be compiled successfully.
24.
Which of these packages contains abstract keyword?
Correct Answer
A. Java.lang
Explanation
The correct answer is java.lang. The java.lang package is a core package in Java that contains fundamental classes and interfaces. The abstract keyword is used to declare abstract classes and methods, and it is commonly found in the java.lang package.
25.
Predict the output
abstract class A {
int i;
abstract void display();
}
class B extends A {
int j;
void display() {
System.out.println(j);
}
}
class Abstract_demo {
public static void main(String args[])
{
B obj = new B();
obj.j=2;
obj.display();
}
}
Correct Answer
B. 2
Explanation
The code snippet defines an abstract class A with an integer variable i and an abstract method display(). Class B extends class A and overrides the display() method to print the value of j. In the main method, an object of class B is created and the value of j is set to 2. The display() method is then called, which prints the value of j, which is 2. Therefore, the output of the code is 2.
26.
If a class inheriting an abstract class does not define all of its function then it will be known as?
Correct Answer
A. Abstract
Explanation
If a class inheriting an abstract class does not define all of its functions, it will be known as an abstract class. An abstract class is a class that cannot be instantiated and is meant to be inherited by other classes. It contains one or more abstract methods, which are methods without a body. These abstract methods must be defined by any class that inherits from the abstract class, otherwise, that class will also be considered abstract. Therefore, if a class inheriting an abstract class does not define all of its functions, it will be known as an abstract class.
27.
Which of these class is not a member class of java.io package?
Correct Answer
A. String
Explanation
The class "String" is not a member class of the java.io package. The java.io package mainly deals with input and output operations, whereas the String class is part of the java.lang package and is used to manipulate strings. Therefore, it is not included in the java.io package.
28.
Which of these classes is used for input and output operation when working with bytes?
Correct Answer
A. InputStream
Explanation
InputStream is the correct answer because it is the base class for all input streams in Java, and it is used for input operations when working with bytes. It provides methods to read bytes from a source, such as a file or network connection. InputStream is commonly used for reading binary data, such as images or audio files, where the data is represented as a sequence of bytes. Reader and Writer classes, on the other hand, are used for character-based input and output operations, not specifically for working with bytes.
29.
Given the following piece of code:
public class Company{
public abstract double calculateSalaries();
}
which of the following statements is true?
Correct Answer
D. Class Company must be defined abstract.
Explanation
The correct answer is that Class Company must be defined abstract. This is because the keyword "abstract" is used in the class declaration, indicating that the class itself is abstract. An abstract class cannot be instantiated and is typically used as a base class for other classes to inherit from. In this case, the abstract class Company provides a blueprint for other classes to implement the calculateSalaries() method.
30.
Public interface Guard{
void doYourJob();
}
abstract public class Dog implements Guard{}
which of the following statements is correct?
Correct Answer
D. This code will compile without any errors.
Explanation
The code will compile without any errors because the class Dog is implementing the interface Guard. Since the interface does not have any abstract methods, the class Dog does not need to implement any methods.
31.
Public class Person{
public void talk(){ System.out.print("I am a Person "); }
}
public class Student extends Person {
public void talk(){ System.out.print("I am a Student "); }
}
what is the result of this piece of code:
public class Test{
public static void main(String args[]){
Person p = new Student();
p.talk();
}}
Correct Answer
B. I am a Student
Explanation
The code creates a new object of the Student class and assigns it to a variable of type Person. Since the talk() method is overridden in the Student class, when the talk() method is called on the object, it prints "I am a Student". Therefore, the result of the code is "I am a Student".
32.
Which of these keywords can be used to prevent Method overriding?
Correct Answer
D. Final
Explanation
The keyword "final" can be used to prevent method overriding. When a method is declared as final in a superclass, it cannot be overridden by any subclass. This means that the subclass cannot provide a different implementation of the method. By using the final keyword, the superclass ensures that the method remains unchanged and cannot be modified in any way by its subclasses.
33.
What is the output of the program final class A {
int i;
}
class B extends A {
int j;
System.out.println(j + " " + i);
}
class inheritance {
public static void main(String args[])
{
B obj = new B();
obj.display();
}
}
Correct Answer
D. Compilation error
Explanation
The program will result in a compilation error because the class B is trying to access the variable 'i' from its superclass A, which is not allowed because 'i' is declared as private in class A. To access the variable 'i' in class B, it should be declared as protected or public in class A.
34.
What will be output?
class Bike{
void run(){System.out.println("running");}
}
class Splender extends Bike{
void run(){System.out.println("running safely with 60km");}
public static void main(String args[]){
Bike b = new Splender();
b.run();
}
}
Correct Answer
C. Running safely with 60km
Explanation
The output will be "running safely with 60km" because the main method creates an instance of the Splender class and assigns it to a reference variable of type Bike. When the run() method is called on this reference variable, it invokes the overridden run() method in the Splender class, which prints "running safely with 60km".
35.
What will be output?
public class Animal{
public void sound(){
System.out.println("Animal is making a sound");
}
}
class Horse extends Animal{
public void sound(){
System.out.println("Neigh");
}
public static void main(String args[]){
Horse obj = new Animal();
obj.sound();
}
}
Correct Answer
C. Compilation Error
Explanation
The given code will result in a compilation error. This is because the object "obj" is declared as a Horse type, but it is being assigned a new instance of the Animal class. Since Horse is a subclass of Animal, it can be assigned to a variable of type Animal. However, the opposite is not true. In this case, the sound() method is overridden in the Horse class, but since the object is of type Animal, it does not have access to the overridden method. Therefore, the code will not compile.