1. What will be the values of x, m, and n after execution of the following statements?
int x, m, n; m =10; n = 15; x = ++m + n++;
- x = 25, m = 10, n = 15
- x = 26, m = 11, n = 16
- x = 27, m = 10, n = 15
- x = 27, m = 11, n = 16
Ans: (b) x = 26, m = 11, n = 16
2. What will be the output of the following code:
int i= 8, j=5; for( ; i-- > ++j ; ) System.out.println(i+""+j);
Ans: 76
3. What will be the output of the following code:
int i= 5, j=2; for( ; i-- > ++j ; ) System.out.println(i+""+j);
Ans: 43
4. What will be the output of the following statement?
System.out.println( 5>8 || 6>6 ? false : true);
Ans: true
5. What will be the output of the following statement?
public class Test {
public static void main(String[] args)
{
String value = "abc";
changeValue(value);
System.out.println(value);
}
public static void changeValue(String a)
{
a = "xyz";
}
}
Ans: abc
6. What will be the output of the following statement?
String str = "Lovely";
System.out.println(str.charAt(5));
Ans: y
7. For this code, which of the following statement is true?
char c = 'a';
switch (c)
{
case 'a':
System.out.println("A");
case 'b':
System.out.println("B");
default:
System.out.println("C");
}
- output will be A
- output will be A followed by B
- output will be A, followed by B, and then followed by C
- code is illegal and therefore will not compile
Ans: (c) output will be A, followed by B, and then followed by C
8. What will be the output of the following statement?
class Base {
int x = 10;
}
class Derived extends Base
{
int x = 20;
}
Base b = new Base();
Derived d = new Derived();
Base bd = new Derived();
The statement System.out.println(b.x+" "+d.x+" "+bd.x)
Ans: 10 20 10
9. What will be the output of the following statement?
public class First_C {
public void myMethod()
{
System.out.println("Method");
}
{
System.out.println(" Instance Block");
}
public void First_C()
{
System.out.println("Constructor ");
}
static {
System.out.println("static block");
}
public static void main(String[] args) {
First_C c = new First_C();
c.First_C();
c.myMethod();
}
}
Ans: static block Instance Block Constructor Method
10. What will be the output of the following statement?
class A {
int i;
}
class B extends A {
int j;
void display()
{
super.i = j + 1;
System.out.println(j + " " + i);
}
}
class inheritance
{
public static void main(String args[])
{
B obj = new B();
obj.i = 1;
obj.j = 2;
obj.display();
}
}
Ans: 2 3
11. What will be the output of the following statement?
interface A
{
int i = 11;
}
class B implements A
{
void methodB()
{
i = 22;
}
}
public class Main
{
public static void main(String[] args)
{
B ref = new B();
ref.methodB();
}
}
- 22
- 11
- Compile time error
- Runtime error
Ans: (c) Compile time error
12. Which of the following keyword is used to declare a class variable in Java?
- private
- static
- final
- default
Ans: static
13. The default value of char type variable is
- ‘\u0020’
- ‘\u00ff’
- “ “
- ‘\u0000’
Ans: (d) ‘\u0000’
14. Which of the following is CORRECT char variable declaration?
- char c = a;
- char c = (int)65.1;
- char c = ’11’;
- None of These;
Ans: (a) char c = (int)65.1;
15. Which of the following statement will print “CSE”?
String s = "ETE OF CSE310";
- System.out.println(s.substring(7, 11));
- System.out.println(s.substring(7, 10));
- System.out.println(s.substring(6, 10));
- System.out.println(s.substring(6, 9));
Ans: (b) System.out.println(s.substring(7, 10));
16. Which of the following statement will print “Java”?
String s = "Hello World Java";
- System.out.println(s.substring(11, 16));
- System.out.println(s.substring(12, 17));
- System.out.println(s.substring(11, 15));
- System.out.println(s.substring(12, 16));
Ans: (a) System.out.println(s.substring(11, 16));
17. Which of the following method of String class is most appropriate for extracting all the characters of String into an array?
- charAt()
- toCharArray()
- getChar()
- getChars()
Ans: (b) toCharArray()
18. Redefining the super class method in the subclass is known as __.
- Dynamic Binding
- Method Overloading
- Method Overriding
- None of These
Ans: (c) Method Overriding
19. Which of the following keyword is used to restrict the accessibility of a method within the enclosing class?
- private
- static
- final
- None of These
Ans: (a) private
20. final keyword can not be used with __.
- method
- class
- local variable
- constructor
Ans: (d) constructor
21. Which of the following for loop declaration is not valid?
- for ( int i = 7; i <= 77; i += 7 )
- for ( int i = 20; i >= 2; – -i )
- for ( int i = 2; i <= 20; i = 2* i )
- for ( int i = 99; i >= 0; i / 9 )
Ans: (d) for ( int i = 99; i >= 0; i / 9 )
22. A class can be declared as ………………………. if you do not want the class to be sub-classed.
- abstract
- final
- static
- super
Ans: (b) final
23. What will be the output of following code?
interface A
{
boolean task(int a, int b);
}
public class Main {
public static void main(String args[])
{
A ref = (n1, n2) -> n1 + 2 == n2;
System.out.println(ref.task(1, 3));
}
}
- false
- true
- Compile time error
- Runtime error
Ans: (c) Compile time error
24. Methods in interface are by default
- Protected
- Public
- Private
- Non-static
Ans: (b) Public
25. What is the output of given code?
interface A {
int x = 12;
}
interface B {
int y = 13;
}
interface C extends A, B
{
static int sum() {
return x + y;
}
}
public class Main implements C {
public static void main(String args[])
{
System.out.println(C.sum());
}
}
- 25
- Compile time error
- Runtime error
- Blank output
Ans: (c) Runtime error
26. What will be the output of following code?
interface A
{
int task(int a, int b);
}
public class Main {
public static void main(String args[])
{
A ref = (n1, n2) -> {
int x = n1 + n2;
int y = n1 - n2;
int z = x + y;
};
System.out.println(ref.task(4, 3));
}
}
- 8
- 0
- Compile time error
- Runtime error
Ans: Compile time error
27. Which of the following is a valid abstract class?
- class A { abstract void test() {} }
- class A { abstract void test(); }
- abstract class A { abstract void test(); }
- public class abstract A { abstract void test(); }
Ans: (c) abstract class A { abstract void test(); }
abstract class A { abstract void test(); }
28. Which of the following declares an abstract method in an abstract Java class?
- public abstract method();
- public abstract void method();
- public void method() {}
- public void abstract Method();
Ans: (b) public abstract void method();
public abstract void method();
29. Which of the following statements regarding abstract classes are true?
- An abstract class can be extended.
- A subclass of a non-abstract superclass can be abstract.
- A subclass can override a concrete method in a superclass to declare it abstract.
- All of the above
Ans: (d) All of the above
30. Suppose A is an abstract class, B is a concrete subclass of A, and both A and B have a default constructor. Which of the following is correct?
- A a = new A();
- A a = new B();
- B b = new A();
- B b = new B();
- 1 and 2
- 2 and 4
- 3 and 4
- 1 and 3
Ans: (b) 2 and 4
31. Which of the following is a correct interface?
- interface A { void print() { } }
- abstract interface A { print(); }
- abstract interface A { abstract void print(); { }}
- interface A { void print(); }
Ans: (d) interface A { void print(); }
32. Which two of the following are legal declarations for abstract classes and interfaces?
- final abstract class Test {}
- public static interface Test {}
- final public class Test {}
- protected abstract class Test {}
- protected interface Test {}
- abstract public class Test {}
- 1 and 2
- 2 and 4
- 3 and 5
- 3 and 6
Ans: (d) 3 and 6
33. What is the output of given code?
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();
}
}
- Hello
- Compile time error
- Welcome
- Runtime error
Ans: (b) Compile time error
34. What is the output of given code?
interface A
{
int x = 10;
}
class B
{
static int y = 13;
}
public class Main extends B implements A {
public static void main(String args[])
{
System.out.println(x + B.y);
}
}
- 10
- 23
- Compile time error
- Runtime error
Ans: (b) 23
35. Which of the following methods not included in OutputStream class.
- write( )
- skip( )
- close( )
- flush( )
Ans: (b) skip( )
36. What will be the output of the following Java code?
import java.io.*;
class Main
{
public static void main(String args[])
{
File obj = new File("/java/system");
System.out.print(obj.getName());
}
}
- java
- system
- java/system
- /java/system
Ans: (b) system
37. What is the output of given code?
import java.io.*;
class MCQ implements Serializable
{
String name = "LPU";
static String dep = "CSE";
public static void main(String args[]) throws Exception {
MCQ o1 = new MCQ();
FileOutputStream fos = new FileOutputStream("serial.txt");
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(o1);
oos.flush();
oos.close();
MCQ.dep = "ECE";
MCQ o2;
FileInputStream fis = new FileInputStream("serial.txt");
ObjectInputStream ois = new ObjectInputStream(fis);
o2 = (MCQ) ois.readObject();
ois.close();
System.out.println("object2: " + MCQ.dep);
}
}
- CSE
- ECE
- Compile time error
- Runtime Exception
Ans: (b) ECE
38. Which of these methods are used to read in from file?
- get()
- read()
- scan()
- readFileInput()
Ans: (b) read()
39. Which of these values is returned by read() method is end of file (EOF) is encountered?
- 0
- 1
- -1
- Null
Ans: (c) -1
40. Which of these exception is thrown by close() and read() methods?
- IOException
- FileException
- FileNotFoundException
- FileInputOutputException
Ans: (a) IOException
41. Which of the following handles the exception when a catch is not used?
- finally
- throw handler
- default handler
- java run time system
Ans: (c) default handler
Default handler is used to handle all the exceptions if catch is not used to handle exception. Finally is called in any case.
42. Which of the following method(s) not included in InputStream class.
- available( )
- reset( )
- flush( )
- close( )
Ans: (c) flush()
43. What is the output of given code?
import java.io.*;
import java.util.*;
public class MCQ
{
public static void main(String args[]) throws FileNotFoundException
{
PrintWriter pw1 = new PrintWriter(new File("LPU.txt"));
pw1.print("LPU");
pw1.print("JALANDHAR");
pw1.flush();
Scanner st = new Scanner(new File("LPU.txt"));
st.useDelimiter("A");
while (st.hasNext())
{
System.out.print(st.next());
}
}
}
- LPU JALANDHAR
- LPU
- JALANDHAR
- LPUJLNDHR
- LPUJALANDHAR
Ans: (d) LPUJLNDHR
44. What will be the output of the following Java program?
import java.io.*;
class MCQ
{
public static void main(String args[])
{
InputStream obj = new FileInputStream("inputoutput.java");
System.out.print(obj.available());
}
}
Note: inputoutput.java is stored in the disk.
- true
- false
- prints number of bytes in stream
- prints number of blank spaces in the file
Ans: (c) prints number of bytes in file
obj.available() returns the number of bytes.
Output:
$ javac filesinputoutput.java
$ java filesinputoutput
1422
(Output will be different in your case)
45. What is the output of given code?
import java.util.*;
class MCQ
{
public static void main(String args[])
{
ArrayList<String> al1 = new ArrayList<String>();
al1.add("hello");
al1.add("hi");
ArrayList<String> al2 = new ArrayList<String>(al1);
al2.add("how");
al2.add("are");
al2.add("you");
al2.retainAll(al1);
System.out.println(al2.get(0));
}
}
- hi
- hello
- how
- Exception
Ans: (b) hello
46. Which collection class allows you to grow or shrink its size and provides indexed access to its elements, but whose methods are not synchronized?
- java.util.ArrayList
- java.util.List
- java.util.LinkedHashSet
- java.util.HashSet
Ans: (a) java.util.ArrayList
ArrayList provide this functionality.
47. Which interface provides the capability to store objects using a key-value pair?
- java.util.Set
- java.util.Map
- java.util.List
- java.util.Collection
Ans: (b) java.util.Map
48. What is the output of given code?
import java.util.*;
class MCQ
{
public static void main(String args[])
{
ArrayList<String> al1 = new ArrayList<String>();
al1.add("hello");
al1.add("hi");
ArrayList<String> al2 = new ArrayList<String>(al1);
al2.add("how");
al2.add("are");
al2.add("you");
System.out.println(al2.get(3));
}
}
- you
- are
- how
- Exception will be raised
Ans: (b) are
49. Which of these is not part of Java’s collection framework?
- Maps
- Array
- Stack
- Queue
Ans: (a) Maps
50. What is the output of given code?
import java.util.*;
class MCQ
{
public static void main(String args[])
{
ArrayList al = new ArrayList();
al.add("hello");
al.add("hi");
ListIterator li = al.listIterator();
li.add("India");
while (li.hasPrevious())
{
System.out.println(li.previous());
}
}
}
- hello
- hi
- India
- Exception will be raised
Ans: (c) India
51. You need to store elements in a collection that guarantees that no duplicates are stored and all elements can be accessed in natural order. Which interface provides that capability?
- java.util.Map
- java.util.Set
- java.util.List
- java.util.Collection
- Ans: (b) java.util.Set
A set is a collection that contains no duplicate elements. The iterator returns the elements in no particular order (unless this set is an instance of some class that provides a guarantee). A map cannot contain duplicate keys but it may contain duplicate values.
52. What is the output of given code?
import java.util.*;
class MCQ
{
public static void main(String args[])
{
TreeSet<Integer> ts2 = new TreeSet<Integer>(new Comparator<Integer>() {
public int compare(Integer o1, Integer o2)
{
return (o2.compareTo(o1));
}
});
ts2.add(1);
ts2.add(3);
ts2.add(2);
ts2.add(2);
System.out.println(ts2);
}
}
- [1, 2, 2, 3]
- [3, 2, 2, 1]
- [3, 2, 1]
- [1, 2, 3]
Ans: (c) [3, 2, 1]
53. Which of these interface must contain a unique element?
- Queue
- List
- Set
- Collection
Ans: (c) Set
Set interface extends collection interface to handle sets, which must contain unique elements.
54. What is the output of given code?
import java.util.ArrayList;
import java.util.ListIterator;
class MCQ {
public static void main(String args[]) {
ArrayList al = new ArrayList();
al.add("hello");
al.add("hi");
ArrayList al2 = new ArrayList(al);
al2.addAll(al);
ListIterator li = al2.listIterator(3);
while (li.hasNext()) {
System.out.println(li.next());
}
}
}
- hi
- hello
- hi hello
- Exception will be raised
Ans: (a) hi
55. What is the output of this program if input given is “Hello stop World”?
class Input_Output {
public static void main(String args[]) throws IOException {
string str;
BufferedReader obj = new BufferedReader(new InputStreamReader(System.in));
do {
str = (char) obj.readLine();
System.out.print(str);
} while(!str.equals("strong"));
}
}
- Hello
- Hello stop
- World
- Hello stop World
Ans: (d) Hello stop World
“stop” will be able to terminate the do-while loop only when it occurs singly in a line.
“Hello stop World” does not terminate the loop.
Output:
$ javac Input_Output.java
$ java Input_Output
Hello stop World
56. Which of these is correct way of inheriting class A by class B?
Ans: class B extends A {}
57. Which of the these is the functionality of ‘Encapsulation’?
Ans: Binds together code and data
58. Output?
class Main {
public static void main(String[] args) {
int[] scores = new int[10];
scores = new int[3];
scores = new int[]{15,34,18,89,21,90};
for(int sco : scores){
System.out.println(sco);
}
}
}
Ans: 15 34 18 89 21 90
59. How will a class protect the code inside it ?
Ans: Using access specifier
60. Which of these is correct way of calling a constructor having no parameters, of superclass A by subclass B?
- super(void);
- superclass.();
- super.A();
- super();
Ans: d
61. interface I{ void f1(); // 1 public void f2(); // 2 protected void f3(); // 3 private void f4(); // 4 } which lines generate compile time errors?
Ans: compiletime error at lines 3,4
62. What is the result of the following code?
public abstract class Bird {
private void fly() { System.out.println("Bird is flying"); }
public static void main(String[] args) {
Bird bird = new Pelican();
bird.fly();
}
}
class Pelican extends Bird {
protected void fly() { System.out.println("Pelican is flying"); }
}
Ans: Bird is flying.
64. output
class Main {
public static void main(String args[]) {
try{
throw 20;
}
catch(Exception e){
System.out.println(e);
}
}
}
Ans: Compiler Error
65. Output
class Main {
public static void main(String args[]) {
try{
throw new Exception("Error");
}
catch(Exception e){
System.out.println("Got the Exception"+e);
}
}
}
Ans: Got the Exceptionjava.lang.Exception: Error
66. output:
class Main {
public static void main(String args[]) {
try{
}
catch(Exception e){
System.out.println("Got the Exception"+e);
}
finally{
System.out.println("Inside finally block");
}
}
}
Ans: Inside finally block
67. What is Collection in Java?
- A group of objects
- A group of classes
- A group of interfaces
- None of the mentioned
Ans: a
68. Given:
import java.util.*; class Main{
public static void append(List list) { list.add("0042");
} public static void main(String[] args) { List<Integer> intList = new ArrayList<Integer>();
append(intList);
System.out.println(intList.get(0));
}
What is the result?
Ans: 0042
69. What will be the output of the program?
TreeSet map = new TreeSet();
map.add("one");
map.add("two");
map.add("three");
map.add("four");
map.add("one");
Iterator it = map.iterator();
while (it.hasNext() )
{
System.out.print( it.next() + " " );
}
Ans: four one three two