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

JavaTest (1)

The document contains a series of Java practice questions covering various concepts such as output predictions, constructor behaviors, method overriding, abstraction, encapsulation, and static blocks. Each question prompts the reader to explain or choose the correct answer regarding Java programming principles. The questions are designed to test understanding of object-oriented programming in Java.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

JavaTest (1)

The document contains a series of Java practice questions covering various concepts such as output predictions, constructor behaviors, method overriding, abstraction, encapsulation, and static blocks. Each question prompts the reader to explain or choose the correct answer regarding Java programming principles. The questions are designed to test understanding of object-oriented programming in Java.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

JAVA PRACTICE QUESTIONS

1. What will be the output of the following code? Explain why.

class Test {
int x = 10;
public static void main(String[] args) {
Test t1 = new Test();
Test t2 = t1;
t2.x = 20;
System.out.println(t1.x);
}
}
O/P:

2. What happens if you declare a constructor private in Java?


O/P:

3. Can a class be both abstract and final? Why or why not?


Provide the valid reasons.

4. What is the output and why?


interface X {
default void show() {
System.out.println("Interface X");
}
}

class Y implements X {
public void show() {
System.out.println("Class Y");
}

Page 1
JAVA PRACTICE QUESTIONS

class Z extends Y {
public void show() {
super.show();
System.out.println("Class Z");
}

public static void main(String[] args) {


Z z = new Z();
z.show();
}
}

5. Which of the following is true about method overriding in Java?

A. It can only be done in the same class


B. It requires changing the method name
C. It occurs between a superclass and a subclass
D. It is not allowed in Java

6. What happens if a subclass in Java has a method with the same signature as
a method in the parent class, but with a more restrictive access modifier?

A. The program compiles successfully


B. Compilation error
C. The method is overloaded
D. It’s ignored during runtime

7. Which of the following best defines abstraction?

A. Hiding data using private variables


B. Binding data and code together
C. Hiding internal details and showing essential features
D. Reusing code through inheritance

8. In Java, which of the following can be abstract?

A. Class only
B. Method only
C. Both class and method
D. Variables

Page 2
JAVA PRACTICE QUESTIONS

9. Which of the following statements is true?

A. A class can implement multiple interfaces and extend one class.


B. A class can extend multiple classes but implement one interface.
C. A class can extend and implement multiple classes.
D. A class can only implement one interface.

10. What will happen if you do not implement all abstract methods of an
interface in your class?

A. Code compiles fine


B. Compilation error
C. The unimplemented method is ignored
D. The interface is removed automatically

11. Which concept allows you to restrict access to certain parts of an object
from outside classes?

A. Inheritance
B. Abstraction
C. Encapsulation
D. Polymorphism

12. Which of the following statements is TRUE about constructors in Java?

A. Constructors have a return type


B. Constructors can be static
C. Constructors must have the same name as the class
D. Constructors can be called directly using the object

13. Which of the following is NOT a valid constructor definition?

A. public Test() {}
B. private Test(int a) {}
C. void Test() {}
D. protected Test(String s) {}

Page 3
JAVA PRACTICE QUESTIONS

14. What is the result of this constructor chaining?

class Demo {
Demo() {
this(5);
System.out.println("Default constructor");
}
Demo(int x) {
System.out.println("Parameterized constructor: " + x);
}
public static void main(String[] args) {
new Demo();
}
}

A. Compilation error
B. Default constructor
C. Parameterized constructor: 5
Default constructor
D. Default constructor
Parameterized constructor: 5

15. What happens if you don’t define any constructor in a class?

A. Compilation fails
B. Java provides a default no-arg constructor
C. Objects cannot be created
D. JVM throws an error at runtime

16. What does the static keyword mean in Java?

A. The member belongs to an instance of the class


B. The member is shared among all instances of the class
C. The member cannot be accessed
D. The member can only be used in a constructor

Page 4
JAVA PRACTICE QUESTIONS

17. What is the output of the following code?

class A {
static {
System.out.println("Static Block");
}

public static void main(String[] args) {


System.out.println("Main Method");
}
}

A. Main Method
B. Static Block
Main Method
C. Main Method
Static Block
D. Compilation error

18. Which of the following statements about static blocks is TRUE?

A. A class can have only one static block


B. Static blocks are executed after the constructor
C. Static blocks execute when the class is loaded
D. Static blocks can access this

19. Create a Parent class and a Child class. The child class should
override a method but also call the parent method using super.

Page 5
JAVA PRACTICE QUESTIONS

Page 6

You might also like