MCQ's

Download as pdf or txt
Download as pdf or txt
You are on page 1of 10

1) Consider the following code snippet

public class Test{


void test(){
MyClass obj=new MyClass();
Obj.name=”jack”;
//Line-1 insert code here
}
}
Public class MyClass{
Int value;
String name;
}
What can be inserted at //Line-1,which will make the object referred to by obj eligible for
garbage collection?
Ans) obj=null;

2) Consider the following class . Will the code compile?


Import java.util.Date;//L1
Package com.hcl.tss;//L2
Class Course{
Date d;
//Class variables, constructors and methods
}
Ans) Class will compile. And also compiles if we inter-change L1 and L2.

3) Which of the following is not a keyword in java?


Ans) main

4) Which two of the statements are true?


Ans) Interfaces cannot have constructors.
One of the application of interfaces is to provide support for multiple inheritance.

5) Which of the following collection objects, we can retrieve the elements in insertion order?
Ans) ArrayList
Vector

6) Class Test1{
Static void display()throws Exception{
System.ou.println(“Hello”);
}
Public static void main(String[] agrs){
Display();
}
}
Which two are the solutions to fix the compilation error in the above program?
Ans) “throws Exception” is declared in method display(). But it does not throw any Exception
within it.
Method display is incorrectly declared.

7) The sleep(int n) method of Thread class is used to pause the execution of the tthread. The
parameter ‘n’ is measured in _____________
Ans) Milli-second.

8) String colors=”Red, Blue, Green, Orange “;


The string is having some set of colors and we need to extract each.Which of the following
technique is best recommended?
Ans) using split() method of String class.
Using string tokenizer class.

9) What is the output of the 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);

Ans) TRUE

10) Which of the following method never throws any exception?


Ans) write() method of FileWriter class.

11) Which of the following statement is true regarding immutable objects?


Ans) state of the object cannot be changed once it is created.
Immutable objects are always thread safe.

12) Which one of the following is the only method of runnable interface?
Ans: run()
13) From which interface the class stack e is implemented from?
Ans: List<E>

14) Which of the following is a valid class name as per coding standards in java?
Ans: MainClass

15) Methods wait() notify() and notifyall() belong to


Ans: java.lang.Object class

16) Which of the following class is immutable?


Ans: String, Integer, Float

17) Which of the following statements are true?


Ans: constructors cannot be overloaded
constructor don't have a return type
constructor runs automatically once the object is created

18) Consider the following code..


class MyException extends Exception {}
public class TestClass {
public void myMethod() throws ****
{
throw new MyException();
}
}
What can replace ****, so that program will compile without any error?
ANS: - MyException
- Exception
- Throwable

19) A collection class should implement__________interface, so that it's elements can be


accessed through enhanced for loop?
ANS: Iterator

20.) What will be the output of the program?


public class Test {
public static void leftshift(int i,int j) {
i<<=j;
}
public static void main(String args[]){
int i=4;j=2;
leftshift(i,j)
System.out.println(i);
}
}
ANS: 4

21) Serializable interface belongs to which package?


ANS: java.io

22) When a class A implements an interface B. what type of relation is created between
them?
ANS: A is-a B

23) class Employee{


private int id;
private String name;
private double salary;
public Employee(int id,String name,int salary){
this.id=id;
this.name=name;
this.salary=salary;
}
public static void incrementSalary(Employee e,int rate){
e.salary=e.salary+e.salary*rate/100.0;
}
public double getSalary(){
return salary;
}
}

public class Test{


public static void main(String[] args){
Employee e=new Employye(101,"Jones",40000);
Employee.incrementSalary(e,20);
System.out.println(e.getSalary());
}
ANS: 48000

24) Which one of the following provides 100% abstraction?


ANS: interface
25) Which one of the following collection object is thread safe.
ANS: Vector

26) what is the output of this program?


import java.io.*;
class files{
public static void main(String args[]){
File obj=new File("/java/system");
System.out.println(obj.getNmae());
ANS: system

27) package a.b.c;


class A{}
On compiling using the command javac -d.A.java,A.class will be placed
ANS: inside 'a' folder which in turn will be inside 'b' folder and that in turn will be inside 'c' folder

28) Two methods having same name and parameter signature, each appearing in base and delivered
classes.
ANS: Overriding

29) class D{
}
class T{
D d;
public Object getD(){ //line 1
return new D(); //line 2
}
}
class S extends T{
@Override
public D getD(){ //line 3
return new D();
}
}
What is the result of compiling the above code?
ANS: Code will have compilation error at line 3

30) Which one of the following statement will not compile?

Note: Assume appropriate import statement is included in the code.


Ans: ArrayList<Float>al;

31) Which lines of code will generate compilation error?

Import java.*;//L1
Public abstract class internalLogic { //L2
Float density = 20.0;//L3
Void do() { //L4
//lot of valid code
Ans: L1,L2,L3,L4

32) Which one of the following is not a valid operator in java?

Ans: 0

33) Which one of the following is valid declaration of a float?

Ans: float f = 1F

34) What is the role of garbage collector?

Ans: De- allocate the memory consumed by the objects which are no more required.

35) What one of the following is the application of File class?

Ans: Check if a file exists


Delete an existing file.
Get the permissions of an existing file

36) Which one of the following in java works like a container, which holds a collection of classes,
interfaces and enumes.

Ans: Package

37) What could be the output of the code snippet?

Class Test{
Public static void main(String[] args) {
int option = 1;
if(option = 1) //L1
System.out.print(“Java is simple”);
else
System.out.print(“Java is complex”);
}
}
Ans: Compilation error at line L1

38) Which access modifier hides data/methods completely from the outside world?

Ans: protected

39) What will be the output of the program?

Public class Foo {


Public static void main(String[] args) {
try {
return;
} finally {
System.out.println(“Finally”);
}
}
}
Ans: Finally

40) Two methods having same name and parameter signature, each appearing in base and
derived classes. The above scenario can be classified under___________

Ans: Hiding

41) Which one of the following is disadvantage of immutable objects?

Ans: New object need to be created every time we try to modify the state of the object. So it
consumes more resources

42) public class Main{


public static void main (String[] args) {
if(args[0].equals("open"))
if(args[1].equals("someone"))
System.out.println("Hello!");
else
System.out.println("Go away"+args[1]);
}
}
Ans: It will throw ArrayIndexOutOfBondsException at runtime.

43) What will happen when you compile and execute the following class?
public class Program{
public static void main(){
System.out.println("Hello");
}
}
ANS: Code will compile but throws an exception at runtime(Main method not found in class
Program)

44) What are the two types of exceptions in java?


ANS: Checked and Unchecked.

45)public class Main


{
public static void main(String[] args) {
String a="Wild";
String b="Irish";
String c="Rose";
String result;
//.concat(b).concat(c);
System.out.println(result);
}
}

Ans) result+a+b+c;

46) public class Point


{
private int x,y;
private Point(int x,int y){
this.x=x;
this.y=y;
}
}
public class Test{
public static void main(String[] args) {
Point p=new Point(10,20);
}
}
Ans) compiler raises error as class with private constructor cannot be instantiated.

47) Which one of the following statement is false?


ANS: Constructor must be always public and private constructor is never allowed in java.

48) Which one of the following array initialization will cause a compiler error?
ANS: int[][]scores=[2,7,6][9,3,45];
49) Operator = = works with which of the following comparison?
ANS: Compare two objects of different types.
50) Which one of the following array declaration and installation is illegal?
ANS: int a[][]=new int[5][4];

51) There is exactly one copy of this variable exists, regardless of how many times the class has
been instantiated.
ANS: static variable

52) public class AndOrNorEx{


Public static void main(String[] str){
Byte x=1;
Byte y=3;
System.out.println(x&y);
}
}
ANS: 1

53) In java Inheritance is implemented by which kind of relationship?


ANS: is-a

54) Which of the following are package naming conventions?


ANS: Must be all in lower case

55) Which two of the following are valid declaration of the standard main() method?
ANS: public static void main(String[] args){}
Public static public void main(String[] arguments){}

56) Which one of the following is true about the finally block?
ANS: Will be always executed whether exception is thrown or not.

57)Which method of TreeMap class is used to add an element to the collection?


ANS: put()

58) How many command line arguments can be passed to main() method in java?
ANS: Theoretically unlimited

59)class Base{
Private Base(){
System.out.print(“Base”);
}
}
Public class test extends Base{
Public Test(){
System.out.println(“Derived”);
}
Public static void main(String[] args){
New Test();
}
}
ANS: Compilation error

59) Identify two important methods of the exception class that are helpful for debugging the
code.
ANS: printStackTrace()

60)

You might also like