Welcome to your Test - 9 Code - 819

Name
Email
Phone
1.

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

IntStream is1 = IntStream.range(1, 3);
IntStream is2 = IntStream.rangeClosed(1, 3);
IntStream is3 = IntStream.concat(is1, is2);
Object val = is3.boxed().collect(Collectors.groupingBy(k->k)).get(3);
System.out.println(val);
 

Select 1 option(s):
2.

Question:

Given:

        Map hm = new ConcurrentHashMap();
        hm.put(null, "asdf");  //1
        hm.put("aaa", null);  //2

        hm = new HashMap();
        hm.put(null, "asdf");  //3
        hm.put("aaa", null);  //4

        List list = new ArrayList();
        list.add(null); //5
        list.add(null); //6

        list = new CopyOnWriteArrayList();
        list.add(null); //7


Which of the above lines will throw NullPointerException?

(Assume that each of the put and add calls are executed as if they are wrapped inside a try/catch block i.e. an exception thrown at //1 will not prevent the execution of //2.)

Select 2 option(s):
3.

Question: What will the following code print when run without any arguments:-

public class TestClass {
public static int m1(int i){
return ++i;
}
public static void main(String[] args) {
int k = m1(args.length);
k += 3 + ++k;
System.out.println(k);
}
}

Select 1 option(s):

4.

Question: Which of the following are valid declarations inside an interface independent of each other?

Select 2 option(s):
5.

Question:

Given:

  public class ValueHolder{
    final int value;

    *INSERT CODE HERE*

}

Which of the following options can be inserted in the above code independent of each other?

Select 2 option(s):
6.

Question: Which of the following statements will print /test.txt when executed on a *nix system?

Select 1 option(s):
7.

Question: Which of the following are valid code fragments:

Select 2 option(s):
8.

Question:

Given:

public class Student {
public static enum Grade{ A, B , C, D, F}    

    private String name;
    private Grade grade;
    public Student(String name, Grade grade){
        this.name = name;
        this.grade = grade;
    }
    public String toString(){
        return name+":"+grade;
    }
    //getters and setters not shown
}

What can be inserted in the code below so that it will print:
{C=[S3:C], A=[S1:A, S2:A]}


List<Student> ls = Arrays.asList(new Student("S1", Student.Grade.A), new Student("S2", Student.Grade.A), new Student("S3", Student.Grade.C));
//INSERT CODE HERE
System.out.println(grouping);


Select 1 option(s):
9.

Question: Which of the following expressions will evaluate to true if preceded by the following code?

String a = "java";
    char[] b = { 'j', 'a', 'v', 'a' };
    String c = new String(b);
    String d = a;

Select 3 option(s):
10.

Question:

Assuming that the following method will always be called with a phone number in the format ddd-ddd-dddd (where d stands for a digit), what can be inserted at //1 so that it will return a String containing "xxx-xxx-"+dddd, where dddd represents the same four digits in the original number?

public static String hidePhone(String fullPhoneNumber){
  //1 Insert code here
}


Select 2 option(s):
11.

Question: What will the following program print?

public class TestClass{
  public static void main(String[] args){
    var str = "111";
    boolean[] bA = new boolean[1];
    if( bA[0] ) str = "222";
    System.out.println(str);
  }
}

Select 1 option(s):
12.

Question: The following method will compile and run without any problems.

public void switchTest(byte x){
switch(x){
case 'b': // 1
default : // 2
case -2: // 3
case 80: // 4
}
}

Select 1 option(s):

13.

Question: What will the following code snippet print?

int count = 0, sum = 0;
do{
if(count % 3 == 0) continue;
sum+=count;
}
while(count++ < 11);
System.out.println(sum);

Select 1 option(s):

14.

Question:

Given:

        var nums = List.of(1, 2, 3, 4, 5, 6, 7).stream();

        Predicate<Integer> p = //a predicate goes here

        Optional<Integer> value = nums.filter(p).reduce((a, b)->a+b);
        value.ifPresent(System.out::println);

Select 2 option(s):
15.

Question: Which of the following command line options can be used to find out all the dependencies of a module while execution?

Note: Although identifying module dependency is not explicitly mentioned in the exam objectives, we have seen questions on the exam that require you to know about this.

Select 1 option(s):
16.

Question:

Consider the following class...

import java.awt.*;
import java.awt.event.*;
class TestFrame extends Frame
{
   String s="Message";
   public static void main(String args[])
   {
      var t = new TestFrame();
      var b = new Button("press me");
      b.addActionListener(new ActionListener()
                  {
                     public void actionPerformed(ActionEvent e)
                     {
                        System.out.println("Message is " +s);
                     }
                  }
                      );
      t.add(b);
   }
}


What will happen when you attempt to compile and run the program?
NOTE: Although this question uses classes from the AWT package, it is not about those class. You may see exam questions referring to classes that are not on the exam objectives. If you see such classes, you can safely assume that the question is not about them.

Select 1 option(s):
17.

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

List<String> names = Arrays.asList("charles", "chuk", "cynthia", "cho", "cici");
int x = names.stream().filter(name->name.length()>4).collect(Collectors.counting());
System.out.println(x);

Select 1 option(s):
18.

Question:

The following code snippet will print true.

String str1 = "one";
String str2 = "two";
System.out.println( str1.equals(str1=str2) );


Select 1 option(s):
19.

Question: Which of the following are valid JDBC URLs?

Select 3 option(s):
20.

Question: Which line will print the string "MUM"?

public class TestClass{
   public static void main(String args []){
      String s = "MINIMUM";
      System.out.println(s.substring(4, 7)); //1
      System.out.println(s.substring(5)); //2
      System.out.println(s.substring(s.indexOf('I', 3))); //3
      System.out.println(s.substring(s.indexOf('I', 4))); //4
   }
}

Select 1 option(s):
21.

Question: What will be printed when the following program is compiled and run?

class Super{
public int getNumber( int a){
return 2;
}
}
public class SubClass extends Super{
public int getNumber( int a, char ch){
return 4;
}
public static void main(String[] args){
System.out.println( new SubClass().getNumber(4) );
}
}

What will be printed?

Select 1 option(s):
22.

Question:

Assuming that a valid integer will be passed in the command line as first argument, which statements regarding the following code are correct?

public class TestClass{
   public static void main(String args[]){
      var x = Integer.parseInt(args[0]);
      switch(x){
         case x < 5 :   System.out.println("BIG"); break;
         case x > 5 :   System.out.println("SMALL");
         default :    System.out.println("CORRECT"); break;
      }
   }
}


Select 1 option(s):
23.

Question:

Identify correct statements about the following code:

public class Counter<T>{  //1
    T t;
    public  <T> int count(T[] ta, T t){  //2
        this.t = t;  //3
        int count = 0;
        for(T x : ta){
            count =  x == t ? count+1: count;//4
        }
        return count;
    }
}


Select 1 option(s):
24.

Question:

Given:

public class Book{
    private String title;
    private Double price;
    public Book(String title, Double price){
        this.title = title;
        this.price = price;
    }
    //accessor methods not shown

What will the following code print when compiled and run?

Book b1 = new Book("Java in 24 hrs", null);
DoubleSupplier ds1 = b1::getPrice;
System.out.println(b1.getTitle()+" "+ds1.getAsDouble());


Select 1 option(s):
25.

Question:

Identify correct statements about the following code:

interface Processor {
   Iterable process();
}

interface ItemProcessor extends Processor{
   Collection process();
}

interface WordProcessor extends Processor{
   String process();
}

interface GenericProcessor extends ItemProcessor, WordProcessor{
    
}


Select 2 option(s):
26.

Question: Which statement regarding the following code is correct?

class A{
   public int i = 10;
   private int j = 20;

}

class B extends A{
   private int i = 30; //1
   public int k = 40;

}

class C extends B{
}

public class TestClass{
   public static void main(String args[]){
      C c = new C();
      System.out.println(c.i); //2
      System.out.println(c.j); //3
      System.out.println(c.k);
   }
}


Select 1 option(s):
27.

Question:

Given:

public class Book{
    String isbn;
    String title;
    public Book(String isbn, String title){
        this.isbn = isbn;
        this.title = title;
    }
    public int compareTo(Book b){
        return this.isbn.compareTo(b.isbn);
    }
    //accessors not shown
}


and the following code snippet:

List<Book> books = getBooksByAuthor("Ludlum");
Collections.sort(books, (b1, b2)->b1.getTitle().compareTo(b2.getTitle())); //1
Collections.sort(books); //2

Assuming that getBooksByAuthor is a valid method that returns a List of Books, which of the following statements is/are true?

Select 1 option(s):
28.

Question: What will the following program print when run?

// Filename: TestClass.java
public class TestClass{
public static void main(String args[] ){ A b = new B("good bye"); }
}
class A{
A() { this("hello", " world"); }
A(String s) { System.out.println(s); }
A(String s1, String s2){ this(s1 + s2); }
}
class B extends A{
B(){ super("good bye"); };
B(String s){ super(s, " world"); }
B(String s1, String s2){ this(s1 + s2 + " ! "); }
}

Select 1 option(s):

29.

Question:

Given that your code is being run in a locale where the language code is fr and country code is CA, which of the following file names represents a valid resource bundle file name?

Select 1 option(s):
30.

Question: Which of the following are valid module definitions?

Select 1 option(s):
31.

Question: What will the following code print when run?

        Deque<Integer> d = new ArrayDeque<>();
        d.push(1);
        d.push(2);
        d.push(3);
        System.out.println(d.remove());
        System.out.println(d.remove());
        System.out.println(d.remove());

Select 1 option(s):
32.

Question:

Replace XXX with a declaration such that the following code will compile without any error or warning.

    public void m1(XXX list)
    {
    Number n = list.get(0);
    }


Select 1 option(s):
33.

Question:

Consider the following classes:

class A {
public int getCode(){ return 2;}
}
class AA extends A {
public void doStuff() {
}
}

Given the following two declarations, which of the options will compile?

A a = null;
AA aa = null;

Select 4 option(s):

34.

Question: Which code fragments will print the last argument given on the command line to the standard output, and exit without any output or exception stack trace if no arguments are given?

1.
public static void main(String args[ ]){
if (args.length != 0) System.out.println(args[args.length-1]);
}
2.
public static void main(String args[ ]){
try { System.out.println(args[args.length-1]); }
catch (ArrayIndexOutOfBoundsException e) { }
}
3.
public static void main(String args[ ]){
int i = args.length;
if (i != 0) System.out.println(args[i-1]);
}
4.
public static void main(String args[ ]){
int i = args.length-1;
if (i > 0) System.out.println(args[i]);
}
5.
public static void main(String args[ ]){
try { System.out.println(args[args.length-1]); }
catch (NullPointerException e) {}
}

Select 3 option(s):

35.

Question: Which of the following are valid at line 1?

public class X{
    //line 1: insert code here.
}

Select 2 option(s):
36.

Question: Which of the following are correct definitions of a repeatable annotation?

Select 1 option(s):
37.

Question:

You are creating an enthu.questionbank module that depends on enthu.internal.db and enthu.external.feeds modules. Which of the following files correctly defines this module?

Select 1 option(s):
38.

Question:

Identify correct statements about the following code:

List<String> vals = Arrays.asList("a", "b");
String join = vals.parallelStream()
        .reduce("_",
                (a, b)->a.concat(b)
                );
System.out.println(join);


Select 1 option(s):
39.

Question:

Consider the standard 'put' method of class HashMap.

public Object put(Object key, Object value);


Select 1 option(s):
40.

Question: Which of the following method(s) of java.util.stream.Stream interface is/are used for reduction?

Select 2 option(s):
41.

Question:

Assuming the following enum declaration and a variable s of type Switch, identify valid code fragments.

public enum Switch{ ON, OFF }

Select 3 option(s):
42.

Question:

Consider the following code:

public class Student {
private Map<String, Integer> marksObtained = new HashMap<String, Integer>();
private ReadWriteLock lock = new ReentrantReadWriteLock();
public void setMarksInSubject(String subject, Integer marks){
// 1 INSERT CODE HERE
try{
marksObtained.put(subject, marks);
}finally{
// 2 INSERT CODE HERE
}
}
public double getAverageMarks(){
// valid code that computes average
}
}


What should be inserted at //1 and //2?

Select 1 option(s):
43.

Question: What will be the result of compiling and running the following program ?

class NewException extends Exception {}
class AnotherException extends Exception {}
public class ExceptionTest{
public static void main(String[] args) throws Exception{
try{
m2();
}
finally{
m3();
}
catch (NewException e){}
}
public static void m2() throws NewException { throw new NewException(); }
public static void m3() throws AnotherException{ throw new AnotherException(); }
}

Select 1 option(s):

44.

Question:

Given:

interface IHello
{
   public int hello(int x, int y);
   public long hello(long x, long y);
}


Which of the following options are valid definitions in a different file assuming that the above definition is already available?

Select 3 option(s):
45.

Question:

In which sequence will the characters a, b, and, c be printed by the following program?

import java.util.* ;
public class ListTest{
public static void main(String args[]){
List s1 = new ArrayList( );
s1.add("a");
s1.add("b");
s1.add(1, "c");
List s2 = new ArrayList( s1.subList(1, 1) );
s1.addAll(s2);
System.out.println(s1);
}
}

Select 1 option(s):

46.

Question:

Given:

public class Book {
   private String title;
   private String genre;
   public Book(String title, String genre){
        this.title = title; this.genre = genre;
   }

   //accessors not shown
}

and the following code:

List<Book> books = Arrays.asList(
        new Book("Gone with the wind", "Fiction"),
        new Book("Bourne Ultimatum", "Thriller"),
        new Book("The Client", "Thriller")
);

List<String> genreList = new ArrayList<>();
//INSERT CODE HERE
System.out.println(genreList);

Which of the following options will correctly make genreList refer to a List containing the genres of the books present in books List?

Select 4 option(s):
47.

Question: Which of these expressions will obtain the substring "456" from a string defined by String str = "01234567"?

Select 1 option(s):
48.

Question: Which of the following declarations are valid?

Select 4 option(s):
49.

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):
50.

Question: Which of the following interface definitions can use Lambda expressions?

Select 1 option(s):
51.

Question:

A programmer is writing a small component that processes a file line by line. The following is the code :

public class LineByLineProcessor {
    
   public void processLines(String fullFilePath) throws Exception
   {
      // declare and initialize "handle" here
      String str = null;
      while( (str = handle.readLine()) != null)
      {
          System.out.println("Processing line : "+str);
      }
      handle.close();
    }
}
    
Which of the given options will declare and initialize handle appropriately?

Select 2 option(s):
52.

Question: What can you do to make the following code compile?

public class TestClass {
    public static void main(String[] args) {
        int[] values = { 10, 20, 30 };
        for( /* put code here */ ){
        }
    }
}

Select 2 option(s):
53.

Question:

Identify correct statements about the following code:

public String[] getDataForUser(String userid) throws ServerException{
    try{
        String dbdata = loadFromDB(userid);
        String filedata = loadFromFile(userid);
        return new String[]{dbdata, filedata};
    }catch(SQLException|IOException se){
        ServerException e = new ServerException("Unable to load data");
        e.setCause(se);
        throw e;
    }
}


NOTE: Some candidates have reported seeing the usage of ServerException in the exam. It is not known what ServerException does it refer to. There is one java.rmi.ServerException class but it does not have any setCause method. ServerException does have a ServerException(String s, Exception ex) constructor, which can be used to store the actual exception. This exception can then be retrieved using the getCause() method.

Our suggestion is to answer it assuming that it refers to java.rmi.ServerException and that it has a setCause method.

Select 1 option(s):