0% found this document useful (0 votes)
49 views109 pages

Practice Set CSE310

The document consists of multiple practice sets containing programming questions primarily focused on Java concepts, including code outputs, error identification, and object-oriented principles. Each question is followed by multiple-choice answers, with the correct answer indicated at the end. The content is structured in a quiz format aimed at assessing knowledge of Java programming and its features.

Uploaded by

akashreddybiyyam
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)
49 views109 pages

Practice Set CSE310

The document consists of multiple practice sets containing programming questions primarily focused on Java concepts, including code outputs, error identification, and object-oriented principles. Each question is followed by multiple-choice answers, with the correct answer indicated at the end. The content is structured in a quiz format aimed at assessing knowledge of Java programming and its features.

Uploaded by

akashreddybiyyam
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/ 109

PRACTICE SET-1

PRACTICE SET-2
PRACTICE SET-3

1) Write the output of the following code:

class Test
{
static int f1(int i)
{
i++;
System.out.println(i);
return 0;
}
public static void main(String []a)
{
int i = f1(20);
System.out.println(i);
}
}

a) 20 0
b) 21 0
c) Garbage-value 0
d) Compile error

Ans) b

2) Write the output of the following code:

public class Simple


{
public static void main(String []a)
{
int value = 25 / 4 + 2 >> 1;
System.out.println(value);
}
}
a) Compile error since decimal value cannot be assigned to integer variable
b) 7
c) 4
d) 9

Ans) c

3) When is the finalize() method invoked:


a) Before garbage collection b) after garbage collection c) depends upon the programmer
d) when program terminates

Ans) a
4) Which of the following is true:
a) Constructor of a class can’t be private b) Constructor does not has return type
c) both (a) & (b) d) Parameters can’t be defined in Constructor signature
Ans) c

5) Which of the lines contain compile error in the below mentioned program:
public class Simple
{
void Simple() //Line-1
{
int i;
System.out.println(i++); //Line-2
}

public static void main(String []a)


{
Simple s;
s.Simple(); //Line-3
}
}
a) Line-1
b) Line-1 & Line-3
c) Line-2 & Line-3
d) No compile errors

Ans) c

6) Determine the output of following when compiled & executed:


public class Simple
{
public static void main(String []a)
{
int scores[][] = new int[][] { {1,2,8,5,3}, {3,4,6}, {5,6,9,4} };
System.out.println(scores.length);
}
}
a) 3 b) 13 c) It will not compile since the size of the array is not mentioned
d) It will not compile since array initialization is incorrect

Ans) a

7) Which of the following is valid array declaration:


a) char two1[][] = new char[3][4] b) char [][]two1 = new char[3][4]
c) both (a) and (b) d) none of above
Ans) c
8) What is the prototype of default constructor for the following:
public class test{}
a) test() b) test(void) c) public test(void) d) all of above

Ans) a

9) What will be the output of the following program when compiled & executed:
int scores[] = {1, 2, 3, 4, 5};
int points[] = new int[5];
for(int val: scores)
{
points[val] = val;
}

for(int i=0; i<points.length; i++){


System.out.println(points[i]);
}

a) 1 2 3 4 5
b) 0 1 2 3 4
c) Compile error
d) Run-time error
Ans) d

10) Which of the following are legal lines of code?


i) float w = (int)888.8; ii) byte x = (byte)1000L;
iii) long y = (byte)100; iv) short z = (char)100;
a) i, ii and iii b) ii and iii c) ii d) all of the above
Ans) d

11. What will be the output?


1. public class Test{
2. public static void main(String args[]){
3. Object myObj = new String[]{"one", "two", "three"};
4. {
5. for(String s : (String[])myObj)
6. System.out.print(s + ".");
7. }
8. }
9. }
A) one.two.three.
B) Compilation fails because of an error at line 3
C) Compilation fails because of an error at line 5
D) An exception is thrown at runtime.
Ans) a

12. What will be the output?


class One{
final int a = 15;
}
class Two extends One{
final int a = 20;
}
public class Test extends Two{
final int a = 30;
public static void main(String args[]){
Test t = new One();
System.out.print(t.a);
}
}
A) 15
B) 20
C) 30
D) Compiler Error
Ans) d

13. Which of the following statements are incorrect?


a) Default constructor is called at the time of declaration of the object if a
constructor has not been defined.
b) Constructor can be parameterized.
c) finalize() method is called when a object goes out of scope and is no longer
needed.
d) finalize() method must be declared protected.
Ans) c

14. Which of the following keyword can be used to implement constructor chaining
concept?
a) “this" keyword
b) "super" keyword
c) both "this" and "super" can be used
d) Neither "this" nor "super" can be used
Ans) c

15. Which of the following statements is true for the following piece of code:
public class School{
public abstract double numberOfStudent();
}
A. The keywords public and abstract cannot be used together.
B. The method numberOfStudent() in class School must have a body.
C. You must add a return statement in method numberOfStudent().
D. Class School must be defined abstract.
Ans) D

16. What is the output of following program?


public class Simple
{
public static void main(String args[])
{
ArrayList<Integer> obj = new ArrayList<Integer>();
obj.add(11);
obj.add(2);
obj.add(3);
obj.add(5);
obj.add(7);
obj.remove(new Integer(7));
obj.remove(2);
for (int i = 0; i < obj.size(); i++)
System.out.print(obj.get(i) + " ");
}
}

a) 11 2 5
b) Compile-time error
c) 11 3 5
d) Run-time error

Ans) a

17. Which of the following methods is used for increasing the capacity of ArrayList object?

a) Capacity()
b) increaseCapacity()
c) incrementCapacity()
d) ensureCapacity()

And) d
18. Which of the following method signature is correct to read the contents of file using
FileInputStream class:

a) int read(int a)

b) int read(byte[] a)
c) Both a) & b)
d) int read(char[] a)

Ans) c

19. Suppose Scanner class is used for reading contents of file. Then which of the following method
can be used to find whether there is further content available to be read or not?

a) isAvailable()
b) hasNext()
c) isNext()
d) isNextAvailable()

Ans) b

20. Predict the output of following Java program


class Main {
public static void main(String args[]) {
try {
int a = 20;
if(a > 10)
throw a;
System.out.println("Hello");
}
catch(int e) {
System.out.println("Got the Exception " + e);
}
}

a) Got the Exception 20


b) Got the Exception 20 Hello
c) Hello Got the Exception 20
d) The program will not compile

Ans) d
PRACTICE SET-4

1. How many abstract method(s) is/are in any functional interface in Java 8?

A.0

B.1

C.2

D.3

Ans: B

2:-

Which of the following statement is false about abstract class and interface?

(a) Both of them can not be instantiated (b) We can put abstract method in both

(c) Both can contain static final variables (d) Both does not have constructor

Ans: D

3:-

Which of the following is correct for overriding

class A

void show()

{----}

if we want to override show method in it's subclass which header of the show method correct

1- public void show(){---}

2- protected void show(){---}

3- private void show(){---}

4- void show(){---}

(a) all 1,2,3,4 (b) Only 1, 2 and 4 (c) Only 1 and 4 (c) Only 4

Ans: b
4:-

Consider the following line of code:

interface I //Line1

void f1(); //Line2

public void f2(); //Line3

protected void f3(); //Line4

private void f4(); //Line5

Which line/lines generate compile time errors?

(a)Compile-time errors at lines 3, 4 and 5

(b)Compile-time error at line 3

(c)Compile-time errors at lines 3 and 4

(d)Compile-time errors at lines 4 and 5

Ans: d

5:-

What will be the second line of output in the following program?

abstract class Value

Value(){

display();

abstract void display();

class Test extends Value

int val;

Test(int val)
{

System.out.println("initializing..");

this.val=val;

void display()

System.out.println("value is: "+val);

public static void main(String ar[])

Test ob=new Test(20);

ob.display();

(a) initializing.. (b) value is: 20 (c) value is: 0 (d) value is: 10

Ans: A

6:-

What will be the output of following line of code?

abstract class Shape{

int a =1;

void show(){

System.out.println("Show1");}

class Rectangle extends Shape{

int a =2;

public void show(){

System.out.println("Show2");}

public class Test{


public static void main(String[] args){

Shape s = new Rectangle();

System.out.print(s.a);

s.show();

}}

(a) 1Show1 (b) 1Show2 (c) 2Show1 (c) 2Show2

Ans: B

7:-

IF class A is a abstract class having only one abstract method. class B(which is also abstract) which
extends A, contain only it's own abstract method .Now a class C which extends B is a concrete class.

Which statement is true about class c?

(a) May override the class B's abstract method

(b) Must override the abstract methods of both the class

(c) Must override the abstract method of B and may override the abstract method of class A.

(d) Must override the abstract method of A and may override the abstract method of class B.

ANs: B

8:-

Which of the following is correct for overriding

class A

public void show()

{----}

if we want to override show method in it's subclass which header of the show method correct

1- public void show(){---}

2- protected void show(){---}

3- private void show(){---}

4- void show(){---}

(a) all 1,2,3,4 (b) Only 1 (c) Only 1 and 2 (c) Only 1 and 4
Ans: B

9:-

Which can be act as a access specifier for nested class?

(a) public (b) Only public and default (c) Only default (d) public, protected, default and private

Ans: D

10:-

Consider the following line of code:

interface I

int k; // Line 1

void f1(); //Line2

default void myMethod() // Line 3

static void anotherMethod(); // Line 4

Which line/lines generate compile time errors?

(a)Compile-time errors at lines 1 and 2

(b)Compile-time error at line 1 and 4

(c)Compile-time errors at lines 2 and 3

(d)Compile-time errors at lines 2 and 4

Ans: B

11:-

Which of the following line/lines of code gives compilation error (choose most appropriate answer)?

interface A

static void methA() // Line 1


{

System.out.println("Static method in interface");

class B

static void methB() // Line 2

System.out.println("Static method in class");

class Test implements A extends B // Line 3

public static void main(String ar[]){

methA(); // Line 4

methB(); // Line 5

(a) Line 1 and Line 2 (b) Line 3 Only (c) Line 3 and Line 4 (d) Line 3, Line 4 and Line 5

Ans: C

12:-

Suppose you want to invoke the show method of class A. Now which is the correct place to put these
two lines(A a1 = new A(); a1.show();) in the code given below?

class Parent{

static int x =10;

Parent()

class A
{

int x = 20;

void show(){

System.out.println(x);

// Place 1

// Place 2

class Test

public static void main(String ar[])

new Parent();

// Place 3

(a) Place 1 (b) Place 2 (c) Place 3 (d) None of these

ANs: B

13. Which exception can be thrown by readObject() method of java.io.ObjectInputStream class?

(a) only IOException

(b) only ClassNotFoundException

(c) Both IOException and ClassNotFoundException

(d) Neither IOException nor ClassNotFoundException

Ans: C

14:-

What is the output of the following program?

class exception_handling
{

public static void main(String args[])

try {

int i, sum;

sum = 10;

for (i = -1; i < 3 ;++i)

{ sum = (sum / i);

System.out.print(i);

catch(ArithmeticException e) { System.out.print("0");

(a) -1

(b) 0

(c) -10

(d) -101

Ans: C

15:-

What will be the second line in file test.txt after executing the following program?

class Test

public static void main(String[] args) throws Exception {


java.io.File f = new java.io.File("test.txt");

java.io.PrintWriter output = new java.io.PrintWriter(f);

output.print("ETE Exam ");

output.println(40);

output.print("LPU ");

output.println(85.5);

output.print('a');

output.close();

(a) LPU

(b) 40 LPU

(c) 85.5 a

(d) LPU 85.5

Ans: D

16:-

In case of multiple catch blocks,______

(a) The superclass exception must be caught first (b) The subclass exception must be caught first

(c) Either super or subclass can be caught first. (d) None of these

Ans: B

17:-

Which overloaded form of write method is not present in java.io.OutputStream class?


(a) write(int )

(b) write(byte [] )

(c) write(byte [], int, int)

(d) write(String)

Ans: d

18:-

Which of the following statement is not true?

(a) try blocks can be nested (b) inner try blocks exception can be caught by outer's try catch block

(c) Outer try block exception can be caught by inner's try catch block (d) variables declared in a try
block are local to that block

Ans: c

19:-

Which of these methods are used to read from file?

a) get()

b) read()

c) scan()

d) readFileInput()

Ans: b

20:-

Which of the following statement is not true?

(a) a program can have try-catch block without finally (b) a program can have try-finally block
without catch

(c) a program can have try block without catch or finally (d) a try block can have multiple catch
statements

Ans: C
21:-

To provide Serialization your class should implements which of the following interface?

(a) java.io.Serializable

(b) java.lang.Serializable

(c) java.io.Serialization

(d) java.lang.Serialization
PRACTICE SET-5

1. Which concept of Java is achieved by combining methods and attribute into a class?
[a]
a) Encapsulation
b) Inheritance
c) Polymorphism
d) Abstraction
2. Which of these selection statements test only for equality? [b]
a) if
b) switch
c) if & switch
d) none of the mentioned

3. What is the output of the give expression System.out.println(12.50 /0.0) [d]


a) 12.50
b) 0.0
c) Double.INFINITY
d) Infinity

4. What will be the output of the following Java program? [d]

char[] chars = new char[] {'\u0065'};

String str = new String(chars);

System.out.println(str);

a) 65
b) A
c) a
d) e

5. Java language was originally developed for operating? [d]


a) TV
b) TV Set-top box
c) Embedded System equipment
d) All the above
6. What is the stored in the object obj in following lines of Java code? [b]
Box obj;

a) Memory address of allocated memory of object


b) NULL
c) Any arbitrary pointer
d) Garbage

7. Which of these is an incorrect array declaration? [d]


a) int arr[] = new int[5]
b) int [] arr = new int[5]
c) int arr[] = new int[5]
d) int arr[] = int [5] new

8. Which of these is correct way of calling a constructor having no parameters, of


superclass A by subclass B? [d]
a) super(void);
b) superclass.();
c) super.A();
d) super();

9. What type of relationship between parent class and child class [c]
a) use-a
b) has-a
c) is-a
d) None of them

10. If super class and subclass have same variable name, which keyword should be
used to use super class? [a]
a) super
b) this
c) upper
d) classname.variableName

11. The functionality of multiple inheritance can be implemented in Java by [c]


i. Extending one class and implementing multiple interfaces.

ii. Implementing multiple interfaces


iii. Extending multiple classes and interfaces.

iv. Extending multiple classes and one interface

a) Only I
b) Only III
c) I & II
d) II & III

12. Functional interfaces can be annotated as [b]


a) @Function
b) @FunctionalInterface
c) @Functional
d) @Interface

13. Which is true about a method-local inner class? [b]


a) It must be marked final.
b) It can be marked abstract.
c) It can be marked public.
d) It can be marked static

14. What is the output of this program? [d]


class string_demo {
public static void main(String args[]) {
String obj = "CSE" + "LPU" + "Punjab";
System.out.println(obj);
}
}
a. CSE
b. LPU
c. Punjab
d. CSELPUPUNJAB

15. Which of the following statements are incorrect? [c]

a) public members of class can be accessed by any code in the program


b) private members of class can only be accessed by other members of the class
c) private members of class can be inherited by a subclass, and become protected
members in subclass
d) protected members of a class can be inherited by a subclass, and become private
members of the subclass
16. Which exception is thrown by the read( ) method of InputStream class. [b]
a) Exception
b) IOException
c) ReadException
d) File Not Found Exception

17. When Exceptions in Java does arises in code sequence? [a]

a) Run Time
b) Can Occur Any Time
c) Compilation Time
d) None of the mentioned

18. Which exception is thrown when divide by zero statement executes? [c]

a) NumberFormatException
b) NullPointerException
c) ArithmeticException
d) None of these

19. Which of these is a super class of all errors and exceptions in the Java language?
[b]

a) Catchable
b) Throwable
c) RunTimeExceptions
d) Checked Exception

20. Which of these keywords must be used to monitor for exceptions? [d]

a) Finally
b) Throw
c) Catch
d) Try
PRACTICE SET -6

Question 1:

public class Demo

public static void main(String[] args) {

byte b=127;

b=(byte)(b+1);

System.out.println(b);

a. -128
b. 127
c. 128
d. Compilation error
Answer: a

Question 2:

public class Demo {

public static void main(String[] args) {

int a = 10;

a = a << 2;

System.out.println(a);

a. 32
b. 8
c. 40
d. 2
Answer: c
Question 3:

Which operator is used to invert all the digits in a binary representation of a number?

a. ~
b. <<<
c. >>>
d. ^
Answer: a

Question 4:

Platform independent code is understandable by?

a. JRE
b. JDK
c. JVM
d. Compiler
Answer: c

Question 5:

public class Demo {

public static void main(String[] args) {

int[] a=new int[0];

System.out.println(a.length);

a. 0
b. Compiler error, arrays cannot be initialised to 0.
c. Compiler error, it is a.length() not a.length
d. None of the above
Answer: a

Question 6:
What is stored in the object obj in following lines of Java code?

Box obj;

a. Memory address of allocated memory of object


b. NULL
c. Any arbitrary pointer
d. Garbage
Answer: b

Question 7:

All classes in Java are inherited from which class?

a. java.lang.class
b. java.class.inherited
c. java.class.object
d. java.lang.Object
Answer: d

Question 8:

Which design pattern ensures that only one object of particular class gets created?

a. filter
b. abstract
c. final
d. Singleton
Answer: d

Question 9:

class Main {

public static void main(String args[]){

final int i;

i = 20;

System.out.println(i);

}
}

a. 20
b. Compiler error
c. 0
d. Garbage value
Answer: a

Question 10:

class Main {

public static void main(String args[]){

final int i;

i = 20;

i = 30;

System.out.println(i);

a. 30
b. Compiler Error
c. Runtime error
d. 0
Answer: b
PRACTICE SET - 7
Question 1:

abstract class AbstractClass

private abstract int abstractMethod();

a. abstract methods can’t be private.


b. The class is not necessary to be abstract
c. The empty curly brackets {} are required to be used instead of ; after the method header.
d. There is no problem with the code.
Answer: a

Question 2:

Nested class can be

a. Static
b. Final
c. Public
d. All
Answer: d

Question 3:

class Demo
{
ArithmeticException ae;
public static void main(String[] args) {
int a=5;
if(a==5)
{
throw ae;
}
System.out.println(a);
}
}
a. 5
b. Error
c. ArithmeticException

d. NullPointerException

Answer: b

Explanation: Non static member cannot be referred from static context.

Question 4:

class Demo
{

public static void main(String[] args) {


ArithmeticException ae;
int a=5;
if(a==5)
{
throw ae;
}
System.out.println(a);
}
}

a. 5
b. Error
c. ArithmeticException

d. NullPointerException

Answer: b

Explanation: Variable ae must have been initialised.

Question 5:

class Demo
{

public static void main(String[] args) {


ArithmeticException ae=null;
int a=5;
if(a==5)
{
throw ae;
}
System.out.println(a);
}
}

a. 5
b. Error
c. ArithmeticException

d. NullPointerException

Answer: d

Question 6:

class Demo
{

public static void main (String[] args) {

Set s=new HashSet();


s.add(1);
s.add(1);
System.out.println(s);
}
}

a. Adding the object ‘1’ for the second time will cause error
b. Adding the object ‘1’ for the second time will through exception
c. Adding the object ‘1’ twice is allowed as set can hold duplicate elements.
d. Adding the object ‘1’ twice will still leave the set holding only single ‘1’.

Answer: d

Question 7:

Odd one out:

a. Set
b. Map
c. Deque
d. Tree

Answer: Map

Question 8:

Which of these access specifiers can be used for an interface?

a. public

b. protected

c. private

d. All of the mentioned

Answer: a

Question 9:

___________ returns true if called on a file and returns false when called on a directory.

A. isDirectory()
B. IsDirectory()
C. isFile()
D. Isfile()

Answer: C

Question 10:

import java.io.*;

class files

public static void main(String args[])

{
File obj = new File(""/java/system"");

System.out.print(obj.getName());

A. java
B. system
C. java/system
D. /java/system

Answer: b
PRACTICE SET-8

Q1
What will be the output of following code?
public class Task
{
public static void main(String[] args)
{
byte a=127;
a++;
System.out.println(a);
}
}
A. 128
B. -128
C. 0
D. Compile time error

Q2

What will be the output of following code?


public class Task
{
public static void main(String[] args)
{
long x=123;
int y=x;
System.out.println(y);
}
}
A. 123
B. 123000
C. Compile time error
D. 0
Q3

What will be the output of following code?


public class Task
{
public static void main(String[] args)
{
int a=6,b=7;
boolean c;
c=++a==b||b++>=8;
System.out.println(c+" "+b);
}
}
A. true 8
B. false 7
C. true 7
D. false 8

Q4
What will be the output of following code?
public class Task
{
public static void main(String[] args)
{
byte b=14;
System.out.println(b>>3);
}
}
A. 112
B. 1
C. 0
D. 17

Q5
What will be the output of following code?
public class First
{
public static void main(String[] args)
{
int a=10;
if(a==11);
System.out.println(++a);
}
}
A. 11
B. 10
C. Compile time error
D. Nothing will be displayed
Q6
Output??
public class First
{
public static void main(String[] args)
{
int a=1;
switch(a)
{
case 1:
a=a+2;
case 2:
a=a*3;
case 3:
a=a/2;
break;
case 4:
a=100;
break;
default:
a=-99;
}
System.out.println(a);
}
}
A. 3
B. 4
C. 100
D. -99

Q7
Output?
public class First
{
public static void main(String[] args)
{
int x=5,sum=0;
boolean y=true;
while(y)
{
sum=sum+x;
x--;
if(x==3)
y=!y;
}
System.out.println(sum);
}
}
A. 9
B. Compile time error
C. Infinite loop
D. 12

Q8
What will be the output of following code?
class Test
{
void myMethod()
{
System.out.println(“ABC");
}
}
public class Derived extends Test
{
void myMethod()
{
System.out.println(“PQR");
}

public static void main(String[] args)


{
Derived object = new Test();
object.myMethod();
}
}
a) ABC
b) PQR
c) Compilation error
d) Runtime error

Q9
What will be the output of following code?
class Test1 {
int p=2;
}
class Test2 extends Test1 {
int q=3;
}
public class CA {
public static void main(String[] args)
{
Test1 obj = new Test2();
System.out.println(obj.p+obj.q);
}
}

A. 5
B. Compile time error
C. 2
D. Runtime error

Q10
Output?
enum Flowers
{
SUNFLOWER,JASMINE,LOTUS;
}
public class Main
{
public static void main(String[] args) {
Flowers var[]=Flowers.values();
for(int i=1;i<2;i++)
System.out.println(var[i]);
}
}
A. JASMINE
B. LOTUS
C. SUNFLOWER
D. 1

Q11
What will be the output of following code?
interface Test
{
boolean check(int p,int q);
}
public class Test{
public static void main(String args[])
{
Test ref=(n1,n2)->n1*2+n2==n1*n2;
System.out.println(ref.check(5,10));
}
}
A. true
B. false
C. Compile time error
D. Runtime error
Q12
What will be the output of following code?
interface Test
{
void show1();
void show2();
}
public class Task
{
public static void main(String[] args)
{
Test obj = new Test()
{
public void show1()
{
System.out.println(“Hello");
}
};
obj.show1();
}
}
A. Hello
B. Compile time error
C. Runtime error
D. Blank Output

Q13
Output??
interface A {
protected int x=12;
public void show();
}
public class Main implements A {
public void show()
{
System.out.println(x);
}
public static void main(String args[])
{
A ref=new Main();
ref.show();
}
}
A. 12
B. 0
C. Compile time error
D. Runtime error

Q14
Output?
interface A {
void show();
void display(){ System.out.println("Welcome");}
}
public class Main implements A {
public void show()
{
System.out.println("Hello");
}
public static void main(String args[])
{
A ref=new Main();
ref.display();
}
}
A. Compile time error
B. Hello
C. Welcome
D. Runtime error

Q15
Output?
class A
{
class B
{
static void methodB()
{
System.out.println("Method B");
}
}
}
public class Main
{
public static void main (String[] args)
{
A obj1=new A();
A.B obj2=obj1.new B();
}
}
A. Method B
B. Compile time error
C. Runtime error
D. Blank output

Q16
Output?
public class Test
{
public static void main(String[] args)
{
try
{
System.out.printf("1");
int sum = 9 / 0;
System.out.printf("2");
}
catch(ArithmeticException e)
{
System.out.printf("3");
}
catch(Exception e)
{
System.out.printf("4");
}
finally
{
System.out.printf("5");
}
}
}
a) 1325
b) 1345
c) 1342
d) 135

Q17
What will happen when you compile and run the following code with assertion enabled?
public class Test{
public static void main(String[] args){

int age = 20;


assert age > 20 : getMessage();
System.out.println("Valid");

}
private static void getMessage() {
System.out.println("Not valid");
}
}
A. The code will not compile
B. The code will compile but will throw AssertionError when executed
C. The code will compile and print Not Valid
D. The code will compile and print Valid
Q18

import java.util.*;
public class Main {
public static void main(String[] args)
{
TreeSet<String> treeSet = new TreeSet<>();
treeSet.add(“B");
treeSet.add(“A");
treeSet.add(“D");
treeSet.add(“C");
for (String temp : treeSet)
System.out.printf(temp + " ");
}
}
OP1. A B C D
OP2. B A D C
OP3. D C B A
OP4. None of these

Q19
Which of the following is true?
1. Iterator can traverse in both forward and backward directions.
2. ListIterator can traverse in both forward and backward directions.
A.Only 1
B.Only 2
C.Both 1 & 2
D.None of the above

Q20
Which of these values is returned by read() method is end of file (EOF) is encountered?
a) 0
b) 1
c) -1
d) Null
PRACTICE SET-9

1. What will be the output of the following Java program?


class string_demo
{
public static void main(String args[])
{
String obj = "I" + "like" + "Java";
System.out.println(obj);
}
}
A. I B. like
C. Java D. IlikeJava

Ans: D

2. When does method overloading is determined?


A. At run time B. At compile time
C. At coding time D. At execution time

Ans: B

3. Given,
ArrayList list = new ArrayList();
What is the initial quantity of the ArrayList list?
A. 5 B. 10
C. 0
D. 100
Ans: B
4. What will be the output of the following Java program?

class string_class
{
public static void main(String args[])
{
String obj = "I LIKE JAVA";
System.out.println(obj.charAt(3));
}
}
A. I B. L
C. K D. E

Ans: A

5. What will be the output of the following Java program?


class string_class
{
public static void main(String args[])
{
String obj = "hello";
String obj1 = "world";
String obj2 = "hello";
System.out.println(obj.equals(obj1) + " " + obj.equals(obj2));
}
}

A. false false B. true true


C. true false D. false true
Ans: D

6. What will be the output of the following Java program?

class output
{
public static void main(String args[])
{
String a = "hello i love java";
System.out.println(a.indexOf('e')+" "+a.indexOf('a')+" "+a.lastIndexOf('l')+"
"+a.lastIndexOf('v'));
}
}
A. 6 4 6 9 B. 5 4 5 9
C. 7 8 8 9 D. 1 14 8 15

Ans: D

7. Which of these is correct way of calling a constructor having no parameters, of superclass


A by subclass B?
A. super(void); B. superclass.();
C. super.A(); D. super();

Ans: D

8. Which among the following violates the principle of encapsulation almost always?
A. Local variables B. Global variables
C. Public variables D. Array variables

Ans: B
9. What is the process of defining a method in a subclass having same name & type signature
as a method in its superclass?
A. Method overloading B. Method overriding
C. Method overriding D. None of the mentioned

Ans: B

10. What will be the output of the following Java code?


class A
{
public int i;
protected int j;
}
class B extends A
{
int j;
void display()
{
super.j = 3;
System.out.println(i + " " + j);
}
}
class Output
{
public static void main(String args[])
{
B obj = new B();
obj.i=1;
obj.j=2;
obj.display();
}
}
A. 1 2 B. 2 1
C. 1 3 D. 3 1

Ans: A

11. How many categories are nested classes divided into?


A. 2 B. 3
C. 4 D. 5

Ans: A

12. How to create object of the inner class?


A. OuterClass.InnerClass innerObject = outerObject.new InnerClass();
B. OuterClass.InnerClass innerObject = new InnerClass();
C. InnerClass innerObject = outerObject.new InnerClass();
D. OuterClass.InnerClass = outerObject.new InnerClass();

Ans: A

13. What will be the output of the following Java code?


class A
{
public int i;
public int j;
A()
{
i = 1;
j = 2;
}
}
class B extends A
{
int a;
B()
{
super();
}
}
class super_use
{
public static void main(String args[])
{
B obj = new B();
System.out.println(obj.i + " " + obj.j)
}
}

A. 1 2 B. 2 1
C. Runtime Error D. Compilation Error

Ans: A

14. What will be the output of the following Java program?


class exception_handling
{
public static void main(String args[])
{
try
{
int a, b;
b = 0;
a = 5 / b;
System.out.print("A");
}
catch(ArithmeticException e)
{
System.out.print("B");
}
finally
{
System.out.print("C");
}
}
}

A. A B. B
C. AC D. BC

Ans: D

15. What will be the output of the following Java program?


class exception_handling
{
public static void main(String args[])
{
try
{
System.out.print("Hello" + " " + 1 / 0);
}
catch(ArithmeticException e)
{
System.out.print("World");
}
}
}

A. Hello B. World
C. HelloWorld D. Hello World

Ans: B

16. Which of these class is used to read characters in a file?


A. FileReader B. FileWriter
C. FileInputStream D. InputStreamReader

Ans: A

17. Which of these type parameters is used for a generic methods to return and accept a
number?
A. K B. N
C. T D. V

Ans: B

18. Which of these data type cannot be type parameterized?


A. Array B. List
C. Map D. Set

Ans: A
19. What are generic methods?
A. Generic methods are the methods defined in a generic class
B. Generic methods are the methods that extend generic class methods
C. Generic methods are methods that introduce their own type parameters
D. Generic methods are methods that take void parameters

Ans: C

20. Which of these method is used to reduce the capacity of an ArrayList object?
A. trim() B. trimSize()
C. trimTosize() D. trimToSize()

Ans: D
PRACTICE SET-10

1. What is the Output of following Java Program?


import java.util.*;
class Demo {
public void show()
{
ArrayList<String> list = new ArrayList<String>();
list.add("banana");
list.add("apple");
Iterator itr = list.iterator();

Collections.sort(list);
while (itr.hasNext()) {
System.out.print(itr.next() + " ");
}
}
}
public class Main {
public static void main(String[] args)
{
Demo demo = new Demo();
demo.show();
}
}
A. Compilation Error cannot give Collections.sort() after Iterator.
B. apple banana
C. banana apple
D. Runtime Error Ans: D

2. What will be the output of the following program?

class demo
{
public static void main(String[] args)
{
int x = 10;
if (++x < 10 && (x / 0 > 10))
{
System.out.println("First CA");
}
else
{
System.out.println("2021");
}
}
}

A.Compile time error


B. RuntimeException:ArithmeticException: / by zero
C. First CA
D. 2021 Ans: D

3. What will be the output of the following Java program?

class demo A. No Output


{ B. 10
public static void main(String args[]) C. Compile time error
{ D. 10 (10 times)
for(int i=0; i<10; i++)
Ans: C
int x = 20;
System.out.println(x);
}
}

4. Which of the following option leads to the portability and security of Java?

A. Bytecode is executed by JVM


B. The applet makes the Java code secure and portable
C. Use of exception handling
D. Dynamic binding between objects Ans: A

5. In Java, local variables are stored in __ memory and instance variables are stored in
___ memory.
A) Stack, Stack
B) Heap, Heap
C) Stack, Heap
D) Heap, Stack Ans: C
6. Which of the following is FALSE about abstract classes in Java?

A. If we derive an abstract class and do not implement all the abstract methods, then
the derived class should also be marked as abstract using 'abstract' keyword
B. Abstract classes can have constructors
C. A class can be made abstract without any abstract method
D. A class can inherit from multiple abstract classes Ans: D
7. What will happen if we provide concrete implementation of method in interface?

A. The concrete class implementing that method need not provide implementation
of that method
B. Runtime exception is thrown
C. Compilation failure
D. Method not found exception is thrown Ans: C

8. Which of the following statements are incorrect?


a. public members of class can be accessed by any code in the program
b. private members of class can only be accessed by other members of the class
c. private members of class can be inherited by a subclass, and become
protected members in subclass
d. protected members of a class can be inherited by a subclass, and become
private members of the subclass. Ans: C

9. 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. Ans: A

A. 1, 2 and 4
B. Only 1 and 2
C. 1, 2 and 3
D. 2, 3 and 4

10. Which is true about an anonymous inner class?


a. It can extend exactly one class and implement exactly one interface.
b. It can extend exactly one class and can implement multiple interfaces.
c. It can extend exactly one class or implement exactly one interface.
d. It can implement multiple interfaces regardless of whether it also extends a class.

Ans: C
11. Which of the following is FALSE for static method?
a. They can only call other static methods.
b. They can only access static data.
c. They can only access dynamic data
d. All of the above are correct. Ans: C

12.
A) PLANE
B) AIRPORT
C) Compiler error Ans: C
D) None
13. Super keyword in java is used to ___________
a. refer current class object.
b. refer static method of the class.
c. refer parent class object.
d. refer static variable of the class. Ans: C

14. What is the return type of lambda expression?


A. String
B. Object Lambda expression enables us to pass functionality as an
C. void argument to another method, such as what action should be
D. no return type Ans: D taken when someone clicks a button.

15. Which of the following statements is true?


i. It is sometimes good practice to throw an AssertionError explicitly.
ii. Private getter() and setter() methods should not use assertions to verify arguments
iii. If an AssertionError is thrown in a try-catch block, the finally block will be bypassed.
iv. It is proper to handle assertion statement failures using a catch (AssertionException ae)
block.

Ans: i
Option A is correct because it is sometimes advisable to thrown an assertion error
even if assertions have been disabled.
Option B is incorrect because it is considered appropriate to check argument values
in private methods using assertions.
Option C is incorrect; finally is never bypassed.
Option D is incorrect because AssertionErrors should never be handled.

16. Which of the mentioned below fall under the category of "checked exceptions" ?

i. Exception
ii. IllegalArgumentException
Option A is the exception base class, which is a checked
iii. IOException
iv. NullPointerException exception. Options B, D,
v. NumberFormatException and E extend RuntimeException directly or indirectly and
vi. StackOverflowError therefore are unchecked exceptions. Option F is a throwable
and not an exception, and so should not be caught or
A. i, ii and iii declared.
B. ii, iv and vi
C. i and iii
D. iii, iv, v

Ans: C

17. Identify which of the following statements are True/False.


i) A catch can have comma-separated multiple arguments.
ii) Throwing an Exception always causes program termination.

A) True, False
B) False, True
C) True, True
D) False, False Ans: D

18.
A. This program will compile successfully.
B. This program fails to compile due to an error at line 4.
C. This program fails to compile due to an error at line 6.
D. This program fails to compile due to an error at line 18. Ans: D

19. Which of the following is False statement regarding the use of generics and parameterized
types in Java?
A. Generics provide type safety by shifting more type checking responsibilities to the
compiler
B. Generics and parameterized types eliminate the need for down casts when using Java
Collections
C. When designing your own collections class (say, a linked list), generics and parameterized
types allow you to achieve type safety with just a single class definition as opposed to
defining
multiple classes
D. All of the mentioned Ans: C

20. Which of these type parameters is used for a generic methods to return and accept any type
of object?
a) K
b) N
c) T
d) V Ans: C
PRACTICE SET -11
Q1:
class CSE310 {
public static void main(String args[]) {
StringBuilder c1 = new StringBuilder("Hello");
StringBuilder c2 = new StringBuilder(" World");
c1.append(c2);
System.out.println(c1);
}
}
a) Hello
b) World
c) Helloworld
d) Hello World
Ans: d

Q2:
What is the string contained in s after following lines of code?
StringBuilder s = new StringBuilder(“Hello lpu students”);
s.delete(5, 9);
A. Hello lpu students
B. Hello students
C. Hello lpu
D. lpu students
Ans: B
Q3:
Which of these method of class StringBuilder is used to concatenate the string representation to the
end of invoking string always?
A. insert()
B. append()
C. join()
D. concatenate()
Ans: B
Q4:
What will s1 contain after following lines of code?
StringBuilder s1 = new StringBuilder(“one”);
StringBuilder s2 = s1.append(“two”)
A. one
B. two
C. onetwo
D. twoone
Ans: C
Q5:
Which of these method of class StringBuilder is used to extract a substring from a String object?
A. substring()
B. Substring()
C. SubString()
D. None of the mentioned
Ans: A
Q6:
Which of the following are incorrect form of StringBuilder class constructor?
A. StringBuilder()
B. StringBuilder(int size)
C. StringBuilder(String str)
D. StringBuilder(int size , String str)
Ans: D
Q7:
What will be the output of the following program code?
class LogicalCompare{
public static void main(String args[]){
String str1 = new String("OKAY");
String str2 = new String(str1);
System.out.println(str1 == str2);
}
}
A. true
B. false
C. 0
D. 1
Ans: b
Q8:
The output of the following fraction of code is
public class Test{
public static void main(String args[]){
String s1 = new String("Hello");
String s2 = new String("Hellow");
System.out.println(s1 = s2);
}
}
A. Hello
B. Hellow
C. Compilation error
D. Throws an exception
Ans: b
Q9: Which of these operators can be used to concatenate two or more String objects?
A. +
B. +=
C. &
D. ||
Ans: A
Q10: Which of these method of class String is used to obtain length of String object?
A. get()
B. size()
C. lengthof()
D. length()
Ans: D

Q11:
All member methods of a local class must be ___________
a) Defined outside class body
b) Defined outside the function definition
c) Defined inside the class body
d) Defined at starting of program
Ans: c
Q12:
What are local classes?
a) Classes declared inside a package
b) Classes declared inside a method
c) Classes declared inside a class
d) Classes declared inside structure
Ans: b
Q13:
Use of nested class ____________ encapsulation.
a) Increases
b) Decreases
c) Doesn’t affect
d) Slightly decreases
Ans: a
Q14:
Non-static nested classes have access to _____________ from enclosing class.
a) Private members
b) Protected members
c) Public members
d) All the members
Ans: d
Q15:
Which among the following best describes a nested class?
a) Class inside a class
b) Class inside a function
c) Class inside a package
d) Class inside a structure
Ans: a
Q16:
public class MyOuter {
public static class MyInner {
public static void foo() { }
}
}
which of the following is correct to create object of nested class?
A. MyOuter.MyInner m = new MyOuter.MyInner();
B. MyOuter.MyInner mi = new MyInner();
C. MyOuter m = new MyOuter();
MyOuter.MyInner mi = m.new MyOuter.MyInner();
D. MyInner mi = new MyOuter.MyInner();
Ans: A
Q17:
class Foo
{
class Bar{ }
}
class Test
{
public static void main (String [] args)
{
Foo f = new Foo();
/* Line 10: Missing statement ? */
}
}
which statement, inserted at line 10, creates an instance of Bar?
A. Foo.Bar b = new Foo.Bar();
B. Foo.Bar b = f.new Bar();
C. Bar b = new f.Bar();
D. Bar b = f.new Bar();
Ans: B

Q18:A method within a class is only accessible by classes that are defined within the same package
as the class of the method.
Which one of the following is used to enforce such restriction?
A. Declare the method with the keyword public.
B. Declare the method with the keyword private.
C. Declare the method with the keyword protected.
D. Do not declare the method with any accessibility modifiers.
E. Declare the method with the keyword public and private.
Ans: D
Q19:
What will be the output for the below code ?
1. class A{
2. public void printValue(){
3. System.out.println("A"); }}
4. class B extends A{
5. public void printValue(){
6. System.out.println("B"); }}
7. public class Test{
8. public static void main(String... args){
9. A b = new B();
10. newValue(b);
11. }
12. public static void newValue(A a){
13. if(a instanceof B){
14. ((B)a).printValue(); } } }
A. A
B. B
C. Compilation fails with an error at line 10
D. Compilation fails with an error at line 14
E. None of these
Ans: B
Q20:
Which design pattern ensures that only one object of particular class gets created?
a) Singleton pattern
b) Filter pattern
c) State pattern
d) Bridge pattern
Ans: a
PRACTICE SET -12

What will be the output of the following Java program? (Note: file is made in c drive.)

import java.io.*;

class files {

public static void main(String args[]) {

File obj = new File("/java/system");

System.out.print(obj.getAbsolutePath());

a) java

b) system

c) C:/java/system

d) C:\java\system

Ans-d

--------------------------------------

2.

In java, how many streams are created for us automatically?

A. 2

B. 3

C. 4

D. 5

Ans : B
---------------------------------------------------

3.

Which method is used to write a byte to the current output stream?

A. public void writ(int)throws IOException

B. public void write(byte[])throws IOException

C. public void flush()throws IOException

D. public void close()throws IOException

Ans-B

---------------------------------------------------

4.

___________ returns true if called on a file and returns false when called on a directory.

A. IsFile()

B. Isfile()

C. isFile()

D. isfile()

Ans-C

---------------------------------------------------

5.

What will be output for the following code?

import java.io.*;

class files

public static void main(String args[])

File obj = new File("/java/system");

System.out.print(obj.getName());
}

A. java

B. system

C. java/system

D. /java/system

Ans-B

---------------------------------------------------

6.

The InputStream class defines methods for performing input functions such as

i) reading bytes

ii) closing streams

iii) skipping ahead in a stream

iv) flushing streams

A) ii, iii and iv only

B) i, ii and iii only

C) i, iii and iv only

D) All i, ii, iii and iv

Ans-B

---------------------------------------------------

7.

The OutputStreams includes methods that are designed to perform the following tasks.

i) closing streams

ii) flushing streams

iii) reading bytes

iv) writing bytes


A) ii, iii and iv only

B) i, ii and iii only

C) i, ii and iv only

D) All i, ii, iii and iv

Ans-C

Predict the output of following Java program

class Main {

public static void main(String args[]) {

try {

throw 10;

catch(int e) {

System.out.println("Got the Exception " + e);

A. Got the Exception 10

B. Got the Exception 0

C. Compiler Error

D. Runtime Error

Ans-C

----------------------------------------------------

2.

class Test extends Exception { }

class Main {

public static void main(String args[]) {

try {
throw new Test();

catch(Test t) {

System.out.println("Got the Test Exception");

finally {

System.out.println("Inside finally block ");

A.

Got the Test Exception

Inside finally block

B. Got the Test Exception

C. Inside finally block

D. Compiler Error

Ans-A

------------------------------------------

3.

Output of following Java program?

class Main {

public static void main(String args[]) {

int x = 0;

int y = 10;

int z = y/x;

A. Compiler Error

B. Compiles and runs fine

C. Compiles fine but throws ArithmeticException exception


D. None of these

Ans- C

Which of the following is a legal identifier in java ?

A. 2variable

B. #@myvar

C. +_$var

D. $_myvar

Ans-D

-------------------------------------------------------------------

2.

Which of the following are legal lines of Java code?

1. int w = (int)888.8;

2. byte x = (byte)100L;

3. long y = (byte)100;

4. byte z = (byte)100L;

A. 1 and 2

B. 1,2 and 4

C. 3 and 4

D. All statements are correct.

Ans-D

-------------------------------------------------------------------

3.

What is the output of this program?

class increment {

public static void main(String args[])

int g = 3;
System.out.print(++g * 8);

} }

a) 25

b) 24

c) 32

d) 33

ANS- C

-------------------------------------------------------------------

4.

What is the output of this program?

class Demo {

public static void main(String args[])

System.out.print(3+5);

System.out.print("Hello"+3+5);

System.out.print(3+5+"Hello");

a) 8 Hello35 8Hello

b) 8 Hello8 8Hello

c) 35 Hello35 35Hello

d) 8 Hello35 35Hello

ANS-A

-------------------------------------------------------------------

5.

Which of these values can a boolean variable contain?

i. 0 and 1

ii. true and false


A. i only

B. ii only

c. i and ii

D. None of these

Ans-B

-------------------------------------------------------------------

6.

Which one is a valid declaration of a boolean?

a) boolean b1 = 1;

b) boolean b2 = ‘false’;

c) boolean b3 = false;

d) boolean b4 = 0 + ‘false’

ans- c

-------------------------------------------------------------------

7.

What is the output of this program?

class array_output {

public static void main(String args[])

char array_variable [] = new char[10];

for (int i = 0; i < 10; ++i) {

array_variable[i] = 'i';

System.out.print(array_variable[i] + "" );

i++;

} } }

a) i i i i i

b) 0 1 2 3 4

c) i j k l m

d) None of the mentioned


ans- a

-------------------------------------------------------------------

8.

What is the output of this program?

class mainclass {

public static void main(String args[])

boolean var1 = true;

boolean var2 = false;

if (var1)

System.out.println(var1);

else

System.out.println(var2);

} }

a) 0

b) 1

c) true

d) false

ans- c

-------------------------------------------------------------------

9.

What is the output of this program?

class asciicodes {

public static void main(String args[])

char var1 = 'A';

char var2 = 'a';

System.out.println((int)var1 + " " + (int)var2);


} }

a) 162

b) 65 97

c) 67 95

d) 66 98

ans-b

-------------------------------------------------------------------

10.

Which of these is an incorrect array declaration?

a) int arr[] = new int[5]

b) int [] arr = new int[5]

c) int arr[] = int [5] new

d) None of these

ans- c
PRACTICE SET -13
1) switch(x) {

default:

System.out.println("Hello");

Which of the following is acceptable types for x?

a) byte and char

b) char and float

c) float and double

d) float and int

1)Which class name among the following is syntactically incorrect

a) public class My Firstclass

b) public class My_FirstClass

c) public class Myfirstclass

d) public class My_First_Class_1

3)What will be the sequence of output if we run the given program with the command java Hello Lpu
Vertos Jalandhar Punjab

class Hello

public static void main(String[]args)

System.out.print(args[0]+" ");

System.out.print(args[2]+" ");

System.out.print(args[3]+" ");

}
}

a. Lpu Vertos Jalandhar

b. Lpu Vertos Jalandhar Punjab

c. Lpu Vertos Punjab

d. Lpu Jalandhar Punjab

4)What will be the result?

int i = 10;

while(i++ <= 10){

i++;

System.out.print(i);

a) 11

b) 12

c) 13

d) 14

5)Identify the output

class test

public static void main(String at[])


{

System.out.println( 13<14 || 14<13 ? false : true);

a)true

b)false

c)false true

d)true false

6)Which of the following is a valid variable declaration?

a)float f = 20.20f;

b)byte b = 300;

c)char c = '67';

d) boolean b =Ttrue;

7)Which of the following is an valid array declaration in java?

a)char char [] = {l, p, u };

b)int ... a;

c)boolean [] var = {false, true, true};

d)None

8)Identify the output

class test

public static void main(String at[])

System.out.println(-99>>3);
}

a)13

b)-13

c)14

d)-14

9)Which of the following will print 17?

a)byte b = -18; System.out.println(~b);

b)byte b = 16; System.out.println(~b);

c)byte b = 18; System.out.println(~b);

d)All the Above

10)What will be the output of the following statement?

byte b = 37;

System.out.println(~b);

a)28

b)-37

c)-38

d)-36

c
11) class Test {

public static void main(String[] args) {

new OuterClass(); // line 3

} }

class OuterClass {

private int x = 9; // line 5

public OuterClass() {

InnerClass inner = new InnerClass(); // line 7

inner.innerMethod();

class InnerClass {

public void innerMethod() {

System.out.println(x);

} } }

choose only one answer:

(A) Prints 9

(B) Error during compilation at line 3

(C) Error during compilation at line 5

(D) Error during compilation at line 7

12) class Body {

String name = "Body";

public static void main(String[] args) {

System.out.println(new Body().new Heart().getPName());

class Heart {

String name = "Heart";

String getPName() {

// insert statement here

}
}

Choose all the correct answers

A) return Body.name;

B) return Body.this.name;

C) return super.name;

D) return this.super.name;

13) class Outer

public static void main(String[] a)

static class Inner{

A) compilation error

B) Runtime error

C) program will execute successfully

D) none of these

14) class AirPlane {

public void fly(int speed) {

final int e = 1;

class FlyingEquation {

System.out.println(e);// line 1

System.out.println(speed);// line 2

} } } }

A) Both statements at lines 1 and 2 are correct

B) Both statements at lines 1 and 2 are incorrect and cause compilation errors
C) Compilation error at line 1 , inner class can't access outer class local variables

D) Compilation error at line 2, inner class can't access method's arguments

15) What is the result of the following code?

1: public abstract class Bird {

2: private void fly() { System.out.println("Bird is flying"); }

3: public static void main(String[] args) {

4: Bird bird = new Pelican();

5: bird.fly();

6: }

7: }

8: class Pelican extends Bird {

9: protected void fly() { System.out.println("Pelican is flying"); }

10: }

A. Bird is flying

B. Pelican is flying

C. The code will not compile because of line 4.

D. The code will not compile because of line 5.

16)What is the output of the following code?

1: public abstract class Whale {

2: public abstract void dive() {};

3: public static void main(String[] args) {

4: Whale whale = new Orca();

5: whale.dive();

6: }

7: }

8: class Orca extends Whale {

9: public void dive(int depth) { System.out.println("Orca diving"); }

10: }

A. Orca diving
B. The code will not compile because of line 2.

C. The code will not compile because of line 8.

D. The code will not compile because of line 9.

17) What is the output of the following code?

1: abstract class Reptile {

2: public final void layEggs() { System.out.println("Reptile laying eggs"); }

3: public static void main(String[] args) {

4: Reptile reptile = new Lizard();

5: reptile.layEggs();

6: }

7: }

8: public class Lizard extends Reptile {

9: public void layEggs() { System.out.println("Lizard laying eggs"); }

10: }

A. Reptile laying eggs

B. Lizard laying eggs

C. The code will not compile because of line 4.

D. The code will not compile because of line 5.

E. The code will not compile because of line 9.

18) Which of the following is true about a concrete subclass? (Choose all that apply)

A. A concrete subclass can be declared as abstract .

B. A concrete subclass must implement all inherited abstract methods.

C. Abstract methods cannot be overridden by a concrete subclass.

D. A concrete subclass cannot be marked as final.

19)What is the output of the following code?

1: class Mammal {

2: public Mammal(int age) {


3: System.out.print("Mammal");

4: }

5: }

6: public class Platypus extends Mammal {

7: public Platypus() {

8: System.out.print("Platypus");

9: }

10: public static void main(String[] args) {

11: new Mammal(5);

12: }

13: }

A. Platypus

B. Mammal

C. PlatypusMammal

D. MammalPlatypus

E. The code will not compile because of line 8.

20)What is the output of the following code?

1: class Arthropod

2: { public void printName(double input) { System.out

.print("Arthropod"); }

3: }

4: public class Spider extends Arthropod {

5: public void printName(int input) { System.out.print("Spider"); }

6: public static void main(String[] args) {

7: Spider spider = new Spider();

8: spider.printName(4);

9: spider.printName(9.0);

10: }

11: }

A. SpiderArthropod
B. ArthropodSpider

C. SpiderSpider

D. ArthropodArthropod

A
PRACTICE SET -14

1. Which of the following interface is inherited by OutputStream class?


i. Cloneable ii. Closeable iii. Flushable

A. i & iii B. ii & iii


C. Only iii D. Only ii

Ans: B

2. Which of the following can be inherited to create a Unchecked User defined


exception?
A. Throwable B. Exception
C. RuntimeException D. Both A & B

Ans: C

3. What will be the Output of the following code snippet?


try{
int sum = 10;
for (int i = -1; i < 3 ;++i)
sum = (sum / i);
System.out.print(sum);
}
catch(Exception e)
{
System.out.print("0");
}

A. -100 B. 0
C. Compilation Error D. No Output

Ans: B

4. Given a User defined Exception class as below:


class MyException extends Exception
{
MyException(String s)
{
super(s);
}
}
What will be the output of the following code snippet?

MyException ob = new MyException("Don't Worry");


System.out.println(ob);

A. Don't Worry B. MyException


C. MyException: Don't Worry D. Compilation Error

Ans: C

5. What will be the Output of the following code snippet?


Map <Integer, String> mp = new HashMap<>();
mp.put(5, "Hello");
mp.put(0, null);
mp.put(3, "Bye");
mp.put(5, "Bye");
System.out.println(mp.entrySet().size() + mp.values().size());

A. 5 B. 6 C. 7 D. None of These

Ans: B

6. Which of the following is TRUE about the following code snippet?


File f = new File("d:/java/txt");
System.out.println(f.delete());

A. prints true and abc will be deleted.


B. prints true but abc will not be deleted
C. prints false and abc will not be deleted
D. Runtime Error

Ans: C

7. Which of the following statement will result in Compilation Error?


class Demo<T extends Comparable, U extends Number>
{}

A. Demo d = new Demo(5, 6.7);


B. Demo <Double, Integer> d = new Demo(1.5, 1);
C. Demo <String, Character> d = new Demo("Hello", 'X');
D. Both A and B

Ans : C

8. What will be the Output of the following code snippet?


Set <Integer> s = new TreeSet <> (
(x, y) -> {
if(x<y) return 1;
else return -1;
}
);
s.add(5); s.add(1); s.add(3); s.add(1);
System.out.println(s);

A. [1, 3, 5] B. [5, 3, 1, 1]
C. [5, 3, 1] D. [1, 1, 3, 5]

Ans: B

9. Which of the following is/are FALSE about Map?


i. In Map, value can be null but key can not be null.
ii. A Map can have duplicate values but can not have duplicate keys.
iii. Default capacity of HashMap is 10 and load factor is 0.75.

A. Only i B. ii & iii


C. i & iii D. Only iii

Ans: C

10. Which of the following is NOT a FUNCTIONAL Interface?


i. Comparator ii. Serializable iii. Set

A. Only i B. i & ii
C. ii & iii D. i & iii

Ans: C

11. Which of the following statement is TRUE about the following code?
public class Outer
{
private int x = 0; //Line 1
private static int y = 0; //Line 2
public class Nested
{
public static void test() //Line 3
{
System.out.println(x + "" +y); //Line 4
}
}
}
A. Line 1 & 2 B. Line 3 & 4
C. Line 3 only D. Line 2, 3 & 4
Ans: C

12. What will be the Output of the following Code snippet?


LocalDate ld = LocalDate.parse("2021-Month.MARCH-20");
System.out.println(ld);

A. 2021-03-20
B. 2021-MAR-20
C. Compilation Error
D. Runtime Error

Ans: D

13. What will be the Output of the following Code snippet?

String s = new String("--this-is-new-island--");


String [] tokens = s.split("-", 4);
System.out.println(tokens[2].length());

A. 2 B. 4 C. 3 D. None of These

Ans: B
14. What will be the Output of the following Code snippet?
String str = new String("Bollywood");
String sub = str.substring(1, 8);
sub = sub.replace('o', 'e');
System.out.println(sub);

A. ellywe B. ellywee
C. ellywo D. ollywo

Ans: B

15. What will be the Output of the following Code snippet?

StringBuilder sb = new StringBuilder(0); //Line 1


sb.append("Hello");
sb.ensureCapacity(8);
System.out.println(sb.capacity());

A. Compilation Error in Line 1


B. 8
C. 12
D. 5

Ans: C
16. What will be the Output of the following Code snippet?

StringBuilder sb = new StringBuilder(6);


sb.ensureCapacity(8);
sb.setLength(2); //Line1
System.out.println(sb.capacity());

A. Compilation Error in Line 1


B. 8
C. 12
D. 14

Ans: D

17. Which of the following is a valid Functional Interface?

i. interface A { abstract void test() {} }


ii. interface A { abstract boolean equals(Object ob); }
iii. interface A { void test();
boolean equals(Object ob); }
iv. interface A { abstract demo(); }

A. i & iv B. only iii C. iii & iv D. ii & iv

Ans: B
18: Which of the following is FALSE about abstract classes in Java?

A. If we inherit an abstract class and do not implement all the abstract methods,
then the derived class must also be declared abstract.
B. Abstract classes can have constructors.
C. A class without any abstract method can be declared abstract class.
D. None of These

Ans: D

19: Which of the following keyword is NOT allowed with Constructor?


i. final ii. static iii. private

A. ii & iii B. i & ii


C. Only ii D. i, ii & iii

Ans: B

20: What will be the output of the following Program?


class A
{
void test(){System.out.println("In A");}
}
class B extends A
{
public void test(){System.out.println("In B");}
}
class Test
{
public static void main(String [] arg)
{
A a1 = new B(); //Line 1
a1.test();
}
}

A. In A B. Compilation Error in Line 1


C. In B D. Runtime Error

Ans: C

You might also like