JavaTest (1)
JavaTest (1)
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:
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");
}
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. Class only
B. Method only
C. Both class and method
D. Variables
Page 2
JAVA PRACTICE QUESTIONS
10. What will happen if you do not implement all abstract methods of an
interface in your class?
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
A. public Test() {}
B. private Test(int a) {}
C. void Test() {}
D. protected Test(String s) {}
Page 3
JAVA PRACTICE QUESTIONS
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
A. Compilation fails
B. Java provides a default no-arg constructor
C. Objects cannot be created
D. JVM throws an error at runtime
Page 4
JAVA PRACTICE QUESTIONS
class A {
static {
System.out.println("Static Block");
}
A. Main Method
B. Static Block
Main Method
C. Main Method
Static Block
D. Compilation error
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