0% found this document useful (0 votes)
13 views9 pages

Practice Test - 4

Its java Practice Test

Uploaded by

deeptasermaraj06
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views9 pages

Practice Test - 4

Its java Practice Test

Uploaded by

deeptasermaraj06
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

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

class Test {
public static void main(String[] args) {
String s1 = "Java";
String s2 = new String("Java");
System.out.println(s1 == s2);
}
}

a) true
b) false
c) compile error
d) runtime error

2. Which constructor call is valid if the class has only a default constructor?

a) new Test();
b) new Test(10);
c) Test();
d) create Test();

3. What is the output of the following code?

class Parent {
void show() { System.out.println("Parent"); }
}
class Child extends Parent {
void show() { System.out.println("Child"); }
}
public class Main {
public static void main(String[] args) {
Parent obj = new Child();
obj.show();
}
}
a) Parent
b) Child
c) Compile error
d) Runtime error

4. Which of these demonstrates compile-time polymorphism?

a) Method overriding
b) Method overloading
c) Inheritance
d) Abstraction

5. What is the output?

class A {
int x = 10;
}
class B extends A {
int x = 20;
}
public class Test {
public static void main(String[] args) {
A obj = new B();
System.out.println(obj.x);
}
}

a) 10
b) 20
c) Compile error
d) Runtime error

6. Which access modifier allows access within the same package but not outside?

a) private
b) protected
c) default
d) public

7. What is the output?

public class Test {


public static void main(String[] args) {
String s = "hello";
s.concat(" world");
System.out.println(s);
}
}

a) hello world
b) hello
c) world
d) compile error

8. Which keyword is used to call a superclass constructor?

a) this
b) super
c) extends
d) base

9. What is the output of this code?

class Test {
public static void main(String[] args) {
try {
int a = 10 / 0;
} catch (ArithmeticException e) {
System.out.println("Exception");
}
System.out.println("After exception");
}
}

a) Exception
b) After exception
c) Exception
After exception
d) Runtime error

10. Which of the following is an abstract method declaration?

a) void show() { }
b) abstract void show();
c) show();
d) void abstract show();

11. What is the output?

class Test {
public static void main(String[] args) {
StringBuffer sb = new StringBuffer("Java");
sb.append(" Rocks");
System.out.println(sb);
}
}

a) Java
b) Java Rocks
c) Rocks
d) Compile error

12. Which exception is checked at compile time?

a) ArithmeticException
b) NullPointerException
c) SQLException
d) ArrayIndexOutOfBoundsException

13. What is the result?

public class Test {


public static void main(String[] args) {
String s = "abc";
System.out.println(s.toUpperCase());
}
}

a) abc
b) ABC
c) compile error
d) runtime error

14. Which collection allows duplicate elements?

a) HashSet
b) ArrayList
c) TreeSet
d) LinkedHashSet

15. What is the output?

class Test {
public static void main(String[] args) {
int arr[] = {1, 2, 3};
for(int i : arr) {
System.out.print(i + " ");
}
}
}
a) 1 2 3
b) 123
c) compile error
d) 3 2 1

16. Which statement about interfaces is true?

a) Interfaces can have constructors.


b) Interfaces can have abstract and default methods
c) Interfaces can extend classes.
d) Interfaces cannot be extended.

17. What is the output?

class Test {
public static void main(String[] args) {
String s1 = "Java";
String s2 = "Java";
System.out.println(s1.equals(s2));
}
}

a) true
b) false
c) compile error
d) runtime error

18. Which collection maintains insertion order and allows duplicates?

a) HashSet
b) TreeSet
c) LinkedHashSet
d) LinkedList
19. Which keyword is used to prevent method overriding?

a) abstract
b) final
c) static
d) const

20. What will happen here?

class Test {
public static void main(String[] args) {
try {
int arr[] = new int[5];
arr[10] = 20;
} catch (ArrayIndexOutOfBoundsException e) {
System.out.println("Error");
}
}
}

a) Error
b) Compile error
c) No output
d) Runtime error without catch

21. Which of the following can throw an exception?

a) int x = 10 / 0;
b) int x = 10 + 0;
c) int x = 10 - 0;
d) int x = 10 * 0;
22. What is the output of this program?

public class Test {


public static void main(String[] args) {
String s = " Java ";
System.out.println(s.trim());
}
}

a) " Java "


b) "Java"
c) "Java "
d) compile error

23. Which collection does not allow null elements?

a) HashSet
b) TreeSet
c) ArrayList
d) LinkedList

24. Which exception will this code throw?

class Test {
public static void main(String[] args) {
String s = null;
System.out.println(s.length());
}
}

a) ArithmeticException
b) NullPointerException
c) ArrayIndexOutOfBoundsException
d) NumberFormatException
25. Which of these is NOT a feature of OOP?

a) Encapsulation
b) Inheritance
c) Polymorphism
d) Compilation

You might also like