Practice Set CSE310
Practice Set CSE310
PRACTICE SET-2
PRACTICE SET-3
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
Ans) c
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
}
Ans) c
Ans) a
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;
}
a) 1 2 3 4 5
b) 0 1 2 3 4
c) Compile error
d) Run-time error
Ans) d
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
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
Ans) d
PRACTICE SET-4
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:-
class A
void show()
{----}
if we want to override show method in it's subclass which header of the show method correct
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:-
interface I //Line1
Ans: d
5:-
Value(){
display();
int val;
Test(int val)
{
System.out.println("initializing..");
this.val=val;
void display()
ob.display();
(a) initializing.. (b) value is: 20 (c) value is: 0 (d) value is: 10
Ans: A
6:-
int a =1;
void show(){
System.out.println("Show1");}
int a =2;
System.out.println("Show2");}
System.out.print(s.a);
s.show();
}}
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.
(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:-
class A
{----}
if we want to override show method in it's subclass which header of the show method correct
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:-
(a) public (b) Only public and default (c) Only default (d) public, protected, default and private
Ans: D
10:-
interface I
int k; // Line 1
Ans: B
11:-
Which of the following line/lines of code gives compilation error (choose most appropriate answer)?
interface A
class B
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{
Parent()
class A
{
int x = 20;
void show(){
System.out.println(x);
// Place 1
// Place 2
class Test
new Parent();
// Place 3
ANs: B
Ans: C
14:-
class exception_handling
{
try {
int i, sum;
sum = 10;
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
output.println(40);
output.print("LPU ");
output.println(85.5);
output.print('a');
output.close();
(a) LPU
(b) 40 LPU
(c) 85.5 a
Ans: D
16:-
(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:-
(b) write(byte [] )
(d) write(String)
Ans: d
18:-
(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:-
a) get()
b) read()
c) scan()
d) readFileInput()
Ans: b
20:-
(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
System.out.println(str);
a) 65
b) A
c) a
d) e
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
a) Only I
b) Only III
c) I & II
d) II & III
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:
byte b=127;
b=(byte)(b+1);
System.out.println(b);
a. -128
b. 127
c. 128
d. Compilation error
Answer: a
Question 2:
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:
a. JRE
b. JDK
c. JVM
d. Compiler
Answer: c
Question 5:
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;
Question 7:
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 {
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 {
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:
Question 2:
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
Question 4:
class Demo
{
a. 5
b. Error
c. ArithmeticException
d. NullPointerException
Answer: b
Question 5:
class Demo
{
a. 5
b. Error
c. ArithmeticException
d. NullPointerException
Answer: d
Question 6:
class Demo
{
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:
a. Set
b. Map
c. Deque
d. Tree
Answer: Map
Question 8:
a. public
b. protected
c. private
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
{
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
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");
}
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){
}
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
Ans: D
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
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
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
Ans: A
Ans: A
Ans: A
A. 1 2 B. 2 1
C. Runtime Error D. Compilation Error
Ans: A
A. A B. B
C. AC D. BC
Ans: D
A. Hello B. World
C. HelloWorld D. Hello World
Ans: B
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
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
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
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");
}
}
}
4. Which of the following option leads to the portability and security of Java?
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
A. 1, 2 and 4
B. Only 1 and 2
C. 1, 2 and 3
D. 2, 3 and 4
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
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
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 {
System.out.print(obj.getAbsolutePath());
a) java
b) system
c) C:/java/system
d) C:\java\system
Ans-d
--------------------------------------
2.
A. 2
B. 3
C. 4
D. 5
Ans : B
---------------------------------------------------
3.
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.
import java.io.*;
class files
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
Ans-B
---------------------------------------------------
7.
The OutputStreams includes methods that are designed to perform the following tasks.
i) closing streams
C) i, ii and iv only
Ans-C
class Main {
try {
throw 10;
catch(int e) {
C. Compiler Error
D. Runtime Error
Ans-C
----------------------------------------------------
2.
class Main {
try {
throw new Test();
catch(Test t) {
finally {
A.
D. Compiler Error
Ans-A
------------------------------------------
3.
class Main {
int x = 0;
int y = 10;
int z = y/x;
A. Compiler Error
Ans- C
A. 2variable
B. #@myvar
C. +_$var
D. $_myvar
Ans-D
-------------------------------------------------------------------
2.
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
Ans-D
-------------------------------------------------------------------
3.
class increment {
int g = 3;
System.out.print(++g * 8);
} }
a) 25
b) 24
c) 32
d) 33
ANS- C
-------------------------------------------------------------------
4.
class Demo {
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.
i. 0 and 1
B. ii only
c. i and ii
D. None of these
Ans-B
-------------------------------------------------------------------
6.
a) boolean b1 = 1;
b) boolean b2 = ‘false’;
c) boolean b3 = false;
d) boolean b4 = 0 + ‘false’
ans- c
-------------------------------------------------------------------
7.
class array_output {
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
-------------------------------------------------------------------
8.
class mainclass {
if (var1)
System.out.println(var1);
else
System.out.println(var2);
} }
a) 0
b) 1
c) true
d) false
ans- c
-------------------------------------------------------------------
9.
class asciicodes {
a) 162
b) 65 97
c) 67 95
d) 66 98
ans-b
-------------------------------------------------------------------
10.
d) None of these
ans- c
PRACTICE SET -13
1) switch(x) {
default:
System.out.println("Hello");
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
System.out.print(args[0]+" ");
System.out.print(args[2]+" ");
System.out.print(args[3]+" ");
}
}
int i = 10;
i++;
System.out.print(i);
a) 11
b) 12
c) 13
d) 14
class test
a)true
b)false
c)false true
d)true false
a)float f = 20.20f;
b)byte b = 300;
c)char c = '67';
d) boolean b =Ttrue;
b)int ... a;
d)None
class test
System.out.println(-99>>3);
}
a)13
b)-13
c)14
d)-14
byte b = 37;
System.out.println(~b);
a)28
b)-37
c)-38
d)-36
c
11) class Test {
} }
class OuterClass {
public OuterClass() {
inner.innerMethod();
class InnerClass {
System.out.println(x);
} } }
(A) Prints 9
class Heart {
String getPName() {
}
}
A) return Body.name;
B) return Body.this.name;
C) return super.name;
D) return this.super.name;
A) compilation error
B) Runtime error
D) none of these
final int e = 1;
class FlyingEquation {
System.out.println(e);// line 1
System.out.println(speed);// line 2
} } } }
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
5: bird.fly();
6: }
7: }
10: }
A. Bird is flying
B. Pelican is flying
5: whale.dive();
6: }
7: }
10: }
A. Orca diving
B. The code will not compile because of line 2.
5: reptile.layEggs();
6: }
7: }
10: }
18) Which of the following is true about a concrete subclass? (Choose all that apply)
1: class Mammal {
4: }
5: }
7: public Platypus() {
8: System.out.print("Platypus");
9: }
12: }
13: }
A. Platypus
B. Mammal
C. PlatypusMammal
D. MammalPlatypus
1: class Arthropod
.print("Arthropod"); }
3: }
8: spider.printName(4);
9: spider.printName(9.0);
10: }
11: }
A. SpiderArthropod
B. ArthropodSpider
C. SpiderSpider
D. ArthropodArthropod
A
PRACTICE SET -14
Ans: B
Ans: C
A. -100 B. 0
C. Compilation Error D. No Output
Ans: B
Ans: C
A. 5 B. 6 C. 7 D. None of These
Ans: B
Ans: C
Ans : C
A. [1, 3, 5] B. [5, 3, 1, 1]
C. [5, 3, 1] D. [1, 1, 3, 5]
Ans: B
Ans: C
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
A. 2021-03-20
B. 2021-MAR-20
C. Compilation Error
D. Runtime Error
Ans: D
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
Ans: C
16. What will be the Output of the following Code snippet?
Ans: D
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
Ans: B
Ans: C