Welcome to the 30 Minutes Java Test

1.

Question: What will the following code fragment print when compiled and run?

 

Statement stmt = null;

Connection c = DriverManager.getConnection("jdbc:derby://localhost:1527/sample", "app", "app");

try(stmt = c.createStatement();)

{

 

    ResultSet rs = stmt.executeQuery("select * from STUDENT");

    while(rs.next()){

        System.out.println(rs.getString(0));

    }

 

}

catch(SQLException e){

    System.out.println("Exception ");

}

(Assume that items not specified such as import statements and try/catch block are all valid.)

 

Select 1 option(s):

2.

Question:

Given:

List<Integer> names = Arrays.asList(1, 2, 3);

How many of the following lines will print exactly 6?

1. System.out.println(names.stream().mapToInt(x->x).sum());

2. System.out.println(names.stream().forEach((sum, x)->sum = sum + x));

3. System.out.println(names.stream().reduce(0, (a, b)->a+b));

4. System.out.println(names.stream().collect(Collectors.mapping(x->x, Collectors.summarizingInt(x->x))).getSum());

5. System.out.println(names.stream().collect(Collectors.summarizingInt(x->x)).getSum());

Select 1 option(s):
3.

Question:

Given:

Stream<String> names = Stream.of("Sarah Adams", "Suzy Pinnell", "Paul Basgall");
Stream<String> firstNames = //INSERT CODE HERE

Which of the following options will correctly assign a stream of just first names to firstNames?

Select 1 option(s)
4.

Question: Which of the following statements are correct regarding synchronization and locks?

Select 2 option(s):
5.

Question: What will the following code print when compiled and run?

public class Onion {
private String data = "skin";

private class Layer extends Onion {
String data = "thegoodpart";
public String getData() {
return data;
}
}

public String getData() {
return new Layer().getData();
}

public static void main(String[] args) {
Onion o = new Onion();
System.out.println(o.getData());
}
}

Select 1 option(s):
6.

Question:

Consider the following classes :

class A{
public static void sM1() { System.out.println("In base static"); }
}

class B extends A{

Line 1 --> // public static void sM1() { System.out.println("In sub static"); }

Line 2 --> // public void sM1() { System.out.println("In sub non-static"); }

}
Which of the following statements are true?

Select 2 option(s):
7.

Question: Which of the following is/are valid functional interfaces?

Select 1 option(s)
8.

Question:

Consider the following code:

class Bond
{
String ticker; double coupon; java.time.LocalDate maturity;
}

class Portfolio implements Serializable
{
String accountName;
Bond[] bonds;
}

public class TestClass {
public static void main(String[] args) throws Exception{
Portfolio portfolio = // get portfolio somehow
// serialize portfolio
}
}
Which of the following approaches can be taken independent of each other so that a Portfolio object can be serialized while preserving the state of the Bond objects contained in Portfolio?

Select 2 option(s):
9.

Question: Which of the following activities are potential targets of denial of service attacks?

Select 1 option(s):
10.

Question: What will the following code print when run?

 

public class TestClass {

    public static void m1() throws Exception{

        throw new Exception("Exception from m1");

    }

    public static void m2() throws Exception{

        try{

            m1();

        }catch(Exception e){

            //Can't do much about this exception so rethrow it

            throw e;

        }finally{

            throw new RuntimeException("Exception from finally");

        }

 

    }

 

    public static void main(String[] args) {

        try{

            m2();

        }catch(Exception e){

            Throwable[] ta = e.getSuppressed();

            for(Throwable t : ta) {

                System.out.println(t.getMessage());

            }

        }

    }   

}

 

Select 1 option(s):

11.

Question: What will the following class print when compiled and run?

import java.util.*;

public class TestClass {

public static void main(String[] args) {

TreeSet<Integer> s = new TreeSet<Integer>();
TreeSet<Integer> subs = new TreeSet<Integer>();

for(int i = 324; i<=328; i++)
{
s.add(i);
}
subs = (TreeSet) s.subSet(326, true, 328, true );
subs.add(329);
System.out.println(s+" "+subs);

}

}

Select 1 option(s):
12.

Question:

Given:

public class TestClass{

static int someMethod(){ return 100; }

static class MyWorker{
Runnable r;
public MyWorker(Runnable r){
this.r = r;
}
}
public static void main(String[] args) {
//INSERT CODE HERE
MyWorker w = new MyWorker(r);
}
}

Select 3 option(s)
13.

Question:

Given the following code snippet:

int rate = 10;
int t = 5;
XXX amount = 1000.0;
for(int i=0; i<t; i++){
amount = amount*(1 - rate/100);
}

What can XXX be?

Select 1 option(s):
14.

Question:

Given that a class named mywids.Widget is part of a module named mod.wids packaged in mywidgets.jar, which of the following commands can be used to execute this class?

(Assume that the jar file is located in the current directory and the jar file does not have any entry in its manifest.)

Select 1 option(s)
15.

Question:

Given:

public class Device implements AutoCloseable{
boolean open = false;
public Device(){
open = true;
}

public String read() throws IOException{
throw new IOException("Can't read!");
}

public boolean isOpened(){
return open;
}

public void close(){
open = false;
System.out.println("Device closed");
}

public static void main(String[] args) {
Device d1 = new Device();
try(d1;
Device d2 = new Device();
Device d3 = new Device()){
d2.read();
}catch(Exception e){
System.out.println("Got Exception "+e.getMessage());
}
}
}

What is the status of Device objects at the end of the try/catch blocks?

Select 1 option(s)
16.

Question:

Consider these two interfaces:

interface I1
{
void m1() throws java.io.IOException;
}
interface I2
{
void m1() throws java.io.FileNotFoundException;
}

Which of the following are valid method declarations for a class that says it implements I1 and I2 ?

Select 1 option(s):
17.

Question:

You have a collection which contains objects of a class X. When the collection is sorted using Collections.sort(collectionOfX);, X's compareTo() method is used.

Which of the following statements are correct about this class X?

Select 2 option(s):
18.

Question:

Given :

//In Data.java
public class Data{
int value;
Data(int value){
this.value = value;
}
}

and the following code fragments:
public void printUsefulData(ArrayList<Data> dataList, Predicate<Data> p){
for(Data d: dataList){
if(p.test(d)) System.out.println(d.value);
}
}

....
ArrayList<Data> al = new ArrayList<Data>();
al.add(new Data(1));al.add(new Data(2));al.add(new Data(3));

//INSERT METHOD CALL HERE

Which of the following options can be inserted above so that it will print 3?

Select 2 option(s)
19.

Question:

Given the class

// Filename: Test.java
public class Test{
   public static void main(String args[]){
      for(int i = 0; i< args.length; i++){
         System.out.print("  "+args[i]);
      }
   }
}

Now consider the following 3 options for running the program:

a: java Test
b: java Test param1
c: java Test param1 param2

Which of the following statements are true?

Select 2 option(s):

20.

Question:

Your application is packaged in myapp.jar and depends on a jar named datalayer.jar, which in turn depends on mysql-connector-java-8.0.11.jar.

You have modularized myapp without waiting for datalayer and mysql driver to be modularized and the following are the contents of myapp's module-info:

module abc.myapp{
requires datalayer;
}

Assming that com.abc.myapp.Main is the main class of your application that you want to execute, which of the following commands will successfully launch your application?

Select 3 option(s)