Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
4 views

MCQ Java

The document contains multiple-choice questions (MCQs) related to Java programming concepts, including constructors, method overriding, exception handling, interfaces, collections, and threading. Each question presents a scenario or code snippet, followed by options for the correct answer. The questions cover fundamental Java principles and common programming practices.

Uploaded by

Suresh
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

MCQ Java

The document contains multiple-choice questions (MCQs) related to Java programming concepts, including constructors, method overriding, exception handling, interfaces, collections, and threading. Each question presents a scenario or code snippet, followed by options for the correct answer. The questions cover fundamental Java principles and common programming practices.

Uploaded by

Suresh
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

MCQ Java

1. Given the following code how could you invoke the Base constructor that
will print out the string "base constructor";

public class Base {


public Base(int i){
System.out.println("base constructor");
}
public Base(){
}
}

public class Sup extends Base{


public static void main(String argv[]){
Sup s= new Sup();
//One
}

public Sup(){
//Two
}

public void derived(){


//Three
}
}

a) On the line After //One put Base(10);


b) On the line After //One put super(10);
c) On the line After //Two put super(10);
d) On the line After //Three put super(10);

2. Which of the following method is not valid when inserted in place of the
comment //Method Here?

class Base{
void amethod(int i) { }
}

public class Derived extends Base{


//Method Here
}

a) void amethod(int i) { }
b) protected void amethod(int i) { }
c) public void amethod(int i) { }
d) private void amethod(int i) { }
3. What is wrong with the following code?
class MyException extends Exception {}
public class Example{
public void doTask() {
try {
bar( );
} finally {
baz( );
} catch (MyException e) {}
}
public void bar( ) throws MyException {
throw new MyException();
}
public void baz( ) throws RuntimeException {
throw new RuntimeException();
}
}

a. Since the method foo( ) does not catch the exception generated by the
method baz( ), it must declare the RuntimeException in a throws clause.
b. A try block cannot be followed by both a catch and a finally block.
c. An empty catch block is not allowed.
d. A catch block cannot follow a finally block.
e. A finally block must always follow one or more catch blocks.

4. Given the following interface definition, which definition is valid?


interface I {
void setValue(int val);
int getValue();
}
a. class A extends I {
int value;
void setValue(int val) { value = val; }
int getValue() { return value; }
}
b. abstract class C implements I {
int getValue() { return 0; }
abstract void increment( );
}
c. interface D implements I {
void increment();
}
d. class E implements I {
int value;
public void setValue(int val) {
value = val;
}
}
5. Given:
public class Person {
private int id;
private String name;
// appropriate constructors
// setters and getters
}
List<Person> persons = new ArrayList<Person>();
persons.add(new Person(100,"Smith"));
persons.add(new Person(120,"Allan"));
persons.add(new Person(301,"Barry"));
persons.add(new Person(101,"Kate"));
Collections.sort(persons);
for(Person person: persons) {
System.out.println(person.getId());
}
What is the output of the above code?
a) Compilation Error
b) Prints: 100, 101, 120 and 301
c) Prints: 120,301,101 and 100
d) Runtime Exception
6) What is the result of compiling and executing the following program.
abstract class Account {
public String getAccountNo() {
return "SB100";
}
}
class SavingsAccount extends Account {
public double getBalance() {
return 0.0;
}
}

public class Test {


public static void main(String peace[]) {
Account account= new SavingsAccount();
System.out.println(account.getBalance());
}
}
a) Compilation error for line [Account account= new SavingsAccount(); ]
b) Compilation error for line [System.out.println(account.getBalance()); ]
c) Prints: 0.0
d) Exception during runtime

7) Given:
Set<String> set = new TreeSet<>();
set.add("E");
set.add("D");
set.add("E");
set.add("B");
set.add("A");

for(String s : set) {
System.out.print(s + " ");
}
What is the output of executing above code?

a) ABDE
b) EDEBA
c) EDBA
d) ABDEE

8) Which of the following are equivalent to the below code?

new Comparator<String>() {
@Override
public int compare(String o1, String o2) {
return o1.length() - o2.length();
}
}
a) (String o1, String o2) ->{ return o1.length() - o2.length();}
b) (o1, o2) ->{ return o1.length() - o2.length(); }
c) (o1, o2) -> o1.length() - o2.length();
d) ALL

9) You want to find out the value of the last element of an array.
You write the following code. What will happen when you compile and run it.?
public class Test{
public static void main(String argv[]){
int[] i = new int[5];
System.out.println(i[5]);
}
}
a) An error at compile time
b) An error at run time
c) The value 0 will be output
d) The string "null" will be output
10) What is the value of "total" variable after executing below code?
int total = Arrays.stream(new int[]{ 1, 2, 3 })
.filter(i -> i >= 2)
.map(i -> i * 3)
.sum();
a) 5
b) 18
c) 15
d) 9

11) Which among the listed methods does not belong to java.lang.Object class?
a) String toString()
b) boolean equals(Object o)
c) Class getClass()
d) int compareTo(Object o)

12) State True or False: static variables reside on Heap?


a) TRUE
b) FALSE
13) Which method is used by the caller thread to wait for other threads to finish
its execution?
a) sleep(long ms)
b) wait()
c) join()
d) suspend()

14) Which method of Object is used to release the lock/monitor/mutex?


a) suspend()
b) sleep(long ms)
c) wait()
d) release()
15) State True or False: Thread class implements Runnable?
a) TRUE
b) FALSE

16) Given the following code:


public static void main(String[] args) {

Integer ints[] = {1,2,3,4,5,6,7};

List<Integer> l = Arrays.asList(ints);

l.forEach(/*here*/);

Which of the following lines must be placed in place of /*here*/ for the number sequence to be
printed?

A. System::out::println

B. System::out.println

C. System.out.println

D. System.out::println

17) Which keyword is used to mark critical section so that at any point only one thread can access
critical section?

a) synchronized

b) transient

c) lock

d) static

18) Which method returns java.sql.ResultSet?

a) executeUpdate( )

b) executeQuery( )

c) getResultSet( )

d) getData( )

19) State True or False: ArithmeticException is a CheckedException


a. True

b. False

20) Which of the following is used to determine at which point an annotation metadata should be

discarded?

a) @Target
b) @Retention

c) @Element

d) We can’t discard annotation metadata

You might also like