0% found this document useful (0 votes)
171 views16 pages

Name - Hrythm R.No. - 1815689: Java MCQ Test - 25ques Coding Questions-2

The document contains 23 multiple choice questions related to Java programming. It tests concepts like default values, operators, access modifiers, inheritance, polymorphism, exceptions, interfaces, threads, files and streams. For each question there are 4-5 answer options and the correct answer is provided. The questions cover Java basics to advanced topics like JDBC and file handling.

Uploaded by

Hrythm Munjal
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
0% found this document useful (0 votes)
171 views16 pages

Name - Hrythm R.No. - 1815689: Java MCQ Test - 25ques Coding Questions-2

The document contains 23 multiple choice questions related to Java programming. It tests concepts like default values, operators, access modifiers, inheritance, polymorphism, exceptions, interfaces, threads, files and streams. For each question there are 4-5 answer options and the correct answer is provided. The questions cover Java basics to advanced topics like JDBC and file handling.

Uploaded by

Hrythm Munjal
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1/ 16

Name - Hrythm

R.No. -1815689

Java MCQ Test -25ques


Coding Questions-2

Question1.

The default value of a static integer variable of a class in Java is,

(a) 0 (b) 1 (c) Garbage value (d) Null (e) -1.

Answer : a

Question 2.

What will be printed as the output of the following program?

public class Test

public static void main(String args[])

int i = 0;

i = i++ + i;

System.out.println("I = " +i);

(a) I = 0 (b) I = 1 (c) I = 2 (d) I = 3

(e) Compile-time Error.

Answer : b
Question 3.

Which statement is not true in java language?

(a) A public member of a class can be accessed in all the packages.

(b) A private member of a class cannot be accessed by the methods of the same class.

(c) A private member of a class cannot be accessed from its derived class.

(d) A protected member of a class can be accessed from its derived class.

(e) None of the above.

Answer : b

Question 4.

To prevent any method from overriding, we declare the method as,

(a) static (b) const (c) final (d) abstract (e) none of the above.

Answer : c

Question5.

Which one of the following is not true?

(a) A class containing abstract methods is called an abstract class.

(b) Abstract methods should be implemented in the derived class.

(c) An abstract class cannot have non-abstract methods.

(d) A class must be qualified as ‘abstract’ class, if it contains one abstract method.

(e) None of the above.

Answer : c

Question 6.

The fields in an interface are implicitly specified as,

(a) static only (b) protected (c) private


(d) both static and final (e) none of the above.

Answer : d

Question.7.

What is the output of the following program:

public class testmeth

static int i = 1;

public static void main(String args[])

System.out.println(i+” , “);

m(i);

System.out.println(i);

public void m(int i)

i += 2;

(a) 1 , 3 (b) 3 , 1 (c) 1 , 1 (d) 1 , 0 (e) none of the


above.

Answer : c

Question 8.

Which of the following is not true?

(a) An interface can extend another interface.

(b) A class which is implementing an interface must implement all the methods of the
interface.
(c) An interface can implement another interface.

(d) An interface is a solution for multiple inheritance in java.

(e) None of the above.

Answer : c

Question 9.

Which of the following is true?

(a) A finally block is executed before the catch block but after the try block.

(b) A finally block is executed, only after the catch block is executed.

(c) A finally block is executed whether an exception is thrown or not.

(d) A finally block is executed, only if an exception occurs.

(e) None of the above.

Answer : c

Question 10.

public class Test

public Test()

System.out.printf("1");

new Test(10);

System.out.printf("5");

public Test(int temp)

System.out.printf("2");

new Test(10, 20);


System.out.printf("4");

public Test(int data, int temp)

System.out.printf("3");

public static void main(String[] args)

Test obj = new Test();

a) 12345

b) Compilation error

c) 15

d) Runtime error

Answer : a

Question 11.

class Test implements Runnable {

public void run()

System.out.println("Run");

}
} class Myclass {

public

static void main(String[] args)

Test t = new Test();

t.start();

System.out.println("Main");

Options:

1. Main Run

2. Run Main

3. Compile time error

4. Depend upon JVM

Answer : 4

Question 12.

What is the output of the following program?

import java.io.IOException;

class Derived

public void getDetails() throws IOException //line 23

System.out.println("Derived class");

}
public class Test extends Derived

public void getDetails() throws Exception //line 24

System.out.println("Test class");

public static void main(String[] args) throws IOException //line 25

Derived obj = new Test();

obj.getDetails();

a) Compilation error due to line 23

b) Compilation error due to line 24

c) Compilation error due to line 25

d) All the above

Answer : b

Question 13.

public class Test

public static void main(String[] args) throws InterruptedException

String str = new String("Hello user");

// making str eligible for gc

str = null;
// calling garbage collector

System.gc();

// waiting for gc to complete

Thread.sleep(1000);

System.out.println("end of main");

@Override

protected void finalize()

System.out.println("finalize method called");

a) finalize method called

b) Run time error

c) Compiler error

d) End main

Answer : d

Question 14.

public class Outer

public static int temp1 = 1;

private static int temp2 = 2;

public int temp3 = 3;

private int temp4 = 4;


public static class Inner

private static int temp5 = 5;

private static int getSum()

return (temp1 + temp2 + temp3 + temp4 + temp5);

public static void main(String[] args)

Outer.Inner obj = new Outer.Inner();

System.out.println(obj.getSum());

a) 15

b) 9

c) 5

d) Compilation Error

Answer : d

Question 15.

class Test1 {

int x = 10;

public static void main(String[] args)

{
System.out.println(x);

static

System.out.print(x + " ");

Option

A) 10 10

B) Error

C) Exception

D) none

Answer : b

Question-16. What will be the output

import java.util.ArrayList;

class Demo {

public void show()

ArrayList<String> list = new ArrayList<String>();

System.out.print(list.get(0));

} public class Main {

public static void main(String[] args)

Demo demo = new Demo();

demo.show();
}

A. ArrayIndexOutOfBoundException

B. IndexOutOfBoundException

C. null

Answer : b

Question 17.

How many JDBC driver types does Sun define?

[A] one

[B] two

[C] three

[D] four

Answer : d

Question 18.

Which type of Statement can execute parameterized queries?

[A] PreparedStatement

[B] ParameterizedStatement

[C] ParameterizedStatement and CallableStatement

[D] All kinds of Statements

Answer : a

Question 19.

What MySQL property is used to create a surrogate key in MySQL? A. UNIQUE B.


SEQUENCE C. AUTO_INCREMENT D. None of the above -- Surrogate keys are not
implemented in MySQL.

[A] UNIQUE
[B] SEQUENCE

[C] AUTO_INCREMENT

[D] None of the above -- Surrogate keys are not implemented in MySQL.

Answer : c

Question 20-What is the output of this program?

class A
{
int i;
int j;
A()
{
i = 1;
j = 2;
}
}
class Output
{
public static void main(String args[])
{
A obj1 = new A ();
A obj2 = new A ();
System.out.print (obj1.equals (obj2));
}
}
a) false
b) true
c) 1
d) Compilation Error
Answer : a

Question 21.What will happen when you attempt to compile and run the following code?
package test;

public class Test extends Thread{


public static void main(String argv[]){
Test t = newTest();
t.run(); // line 4
t.start();
}
publicvoid run(){
System.out.println("run-test");
}
}
A.run-test run-test
B.run-test
C.Compilation fails due to an error on line 4
D.None of these
Answer : a

Question 22.
What will be the output of following code?

public static void main(String[] args){


String parent = null;
File file = new File(parent, "myfile.txt");
try {
file.createNewFile ();
} catch (IOException e) {
e.printStackTrace();
}}
a.compile time error
b.Run time error
c.It will create the file myfile.txt in the current directory.
d.reading data from the file
Answer : a

Question 23.

package test;

importjava.io.*;
public class MyObjectFileStore
{
Public void storeObject(Employee emp)
{
OutputStream ops = null;
ObjectOutputStreamobjOps = null;
try {
ops = newFileOutputStream("MyEmpFile.txt");
objOps = newObjectOutputStream(ops);
objOps.writeObject(emp);
objOps.flush();
} catch (Exception e) {
e.printStackTrace();
}

Public void displayObjects()


{

InputStreamfileIs = null;
ObjectInputStreamobjIs = null;
try {
fileIs = newFileInputStream("MyEmpFile.txt");
objIs = newObjectInputStream(fileIs);
Employee emp = (Employee) objIs.readObject();
System.out.println(emp);
} catch (Exception e) {
e.printStackTrace();

publicstaticvoid main(String a[]){

MyObjectFileStore mof = newMyObjectFileStore();


Employee e1 = newEmployee("Suman",1,"1000");
mof.storeObject(e1);
mof.displayObjects();
}
}

Class Employee {

private String name;


privateint id;
private String salary;

public Employee(String name, int id, String salary){


this.name = name;
this.id = id;
this.salary = salary;
}

public String toString(){


return name +"=="+id+"=="+salary;
}

public String getName() {


return name;
}
publicvoidsetName(String name) {
this.name = name;
}

publicintgetId() {
return id;
}

publicvoidsetId(int id) {
this.id = id;
}

public String getSalary() {


return salary;
}

publicvoidsetSalary(String salary) {
this.salary = salary;
}
}

a.Code reading and writing object into file

b.Run time Error

c.Code saving object in to file

d.none of the above

Answer : a

Question 24.

How many abstract classes can a single program contain?

a) At most 1

b) At least 1

c )At most 127

d )As many as required

Answer : d

Question 25.
If two classes combine some private data members and provides public member functions to
access and manipulate those data members. Where is abstraction used?

A) Using private access specifier for data members

B) Using class concept with both data members and member functions

C) Using public member functions to access and manipulate the data member

D) Data is not sufficient to decide what is being used

Answer : c

You might also like