Welcome to your Test - 6 Code - 819

Name
Email
Phone
1.

Question:

Given:

public class Buddy {
    Buddy upper;
    String name;
    public Buddy(){ }
    
    public Buddy(String name){
        this.name = name;
    }
    public Buddy(String name, Buddy upper){
        this.name =  name;
        Buddy b = new Buddy(upper.name);//1
        upper = b; //2
    }
    
    public static void main(String[] args) {
        Buddy b1 = new Buddy("A");     //3
        Buddy b2 = new Buddy("B", b1); //4
        System.out.println(b1); //5
    }
}

Identify correct statements.

Select 2 option(s):
2.

Question:

Given:

enum Coffee
{
    ESPRESSO("Very Strong"), MOCHA("Bold"), LATTE("Mild");
    public String strength;
    Coffee(String strength)
    {
        this.strength = strength;
    }
}

and the statement
System.out.println(Coffee.ESPRESSO);

What can be done so that the above statement will print Very Strong?

Select 1 option(s):
3.

Question:

You have just taken over as the build manager for your development team and while go through all the build steps you notice that one of the steps of your build involves running a tool over your java files for preprocessing. This tool runs with a security file containing the following code:

grant codeBase "file:${toolhome}/tool.jar",  {
    permission java.security.AllPermission;
};


What should you do?

Note: Although security policy file is not mentioned in the exam objectives explicitly, we have seen similar question in the exam.

Select 1 option(s):
4.

Question:

Consider the following piece of code, which is run in an environment where the default locale is English - US:

        Locale.setDefault(new Locale("fr", "CA")); //Set default to French Canada
        Locale l = Locale.getDefault();
        ResourceBundle rb = ResourceBundle.getBundle("appmessages", l);
        String msg = rb.getString("greetings");
        System.out.println(msg);

You have created two resource bundles for appmessages, with the following contents:

#In English US resource bundle file
greetings=Hello

#In French CA resource bundle file
greetings=bonjour


What will be the output?

Select 1 option(s):
5.

Question:

Given:

List<Double> dList = Arrays.asList(10.0, 12.0);
DoubleFunction df = x->x+10;
dList.stream().forEach(df);
dList.stream().forEach(d->System.out.println(d));

What will it print when compiled and run?

Select 1 option(s):
6.

Question:

Given:

        char a = 'a', b = 98; //1
        int a1 = a; //2
        int b1 = (int) b; //3
        System.out.println((char)a1+(char)b1); //4

What is the output?

Select 1 option(s):
7.

Question:

Given:

interface Book{
  public default String getId(){
     return "ISBN123456";
  }
}

interface Encyclopedia extends Book{
   //INSERT CODE HERE
}

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

Select 2 option(s):
8.

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

Question:

Consider the following code appearing in a file named TestClass.java:

class Test{ }  // 1

public class TestClass {

   var v1; // 2

   public int main(String[] args)  { // 3

       var v2; //4

       double x=10, double y;  // 5

       System.out.println[]; // 6
       
       for(var k=0; k<x; k++){ } //7
        
       return 0;
   }
}

Which of the lines are valid?

Select 3 option(s):
10.

Question:

Given:

import java.util.*;

class Student{
   int marks;
}  

public class TestClass {
   static var getHighest(var students){
       int highest = 0;
       for(var student : students){
          if(highest <student.marks) highest  = s.marks;
       }
       return highest;
   }

   public static void main(String[] args)  {

       var student  = new Student();

       var allStudents = new ArrayList<Student>();  

       allStudents.add(student);

       var h = getHighest(allStudents);
       
       System.out.println(h);
   }

}

Identify correct statements about the above code.

Select 1 option(s):
11.

Question: What will the following class print ?

class InitTest{
   public static void main(String[] args){
      int a = 10;
      int b = 20;
      a += (a = 4);
      b = b + (b = 5);
      System.out.println(a+ ",  "+b);
   }
}

Select 1 option(s):
12.

Question: What should be the return type of the following method?

public RETURNTYPE methodX( byte by){
var d = 10.0;
return (long) by/d*3;
}

Select 1 option(s):

13.

Question: What will be the result of attempting to compile and run the following program?

class TestClass{
public static void main(String args[]){
var i = 0;
for (i=1 ; i<5 ; i++) continue; //(1)
for (i=0 ; ; i++) break; //(2)
for ( ; i<5?false:true ; ); //(3)
}
}

Select 1 option(s):

14.

Question:

Given the following code:

import java.util.Arrays;
import java.util.List;
public class TestClass {
    public static void main(String[] args) {
        List<Integer> al = Arrays.asList(100, 200, 230, 291, 43);

        System.out.println( *INSERT CODE HERE* );
    }
}

Which of the following options will correctly print the number of elements that are less than 200?

Select 1 option(s):
15.

Question: Which secure programming guideline does the following code violate?

    public void saveUserCredentials(String data, String file) throws Exception {
        FileWriter fw = null;
        try{
            fw = new FileWriter(file);
            fw.write(data);
            fw.close();
        }catch(Exception e){
            System.err.println("Unable to save user data "+data+" to "+file);
            e.printStackTrace();
            throw e;
        }finally{
            if(fw != null) fw.close();
        }
    }

Select 3 option(s):
16.

Question: Which of the following correctly identify the differences between a Callable and a Runnable?

Select 3 option(s):
17.

Question:

A developer has written the following code snippet:

        Console c = System.console(); //1
        String line = c.readLine("Please enter your name:"); //2
        System.out.println("Hello, "+line); //3


Which of the following checked exceptions may be thrown by this code?

Select 1 option(s):
18.

Question:

Consider the following class...

class Test{
   public static void main(String[ ] args){
      var a = new int[]{ 1, 2, 3, 4 };
      int[] b = { 2, 3, 1, 0 };
      System.out.println( a [ (a = b)[3] ] );
   }
}


What will it print when compiled and run ?

Select 1 option(s):
19.

Question: What will the following code print?

        List<String> ls = Arrays.asList("Tom Cruise", "Tom Hart", "Tom Hanks", "Tom Brady");
        Predicate<String> p = str->{
            System.out.println("Looking...");
            return str.indexOf("Tom") > -1;
        };
        boolean flag = ls.stream().filter(str->str.length()>8).allMatch(p);

Select 1 option(s):
20.

Question:

Given:

class Base{
  public List<? super CharSequence> getList(int id){
      return null;
  };
}

class Derived extends Base{
  @Override
  *INSERT CODE HERE*
      //valid code
   }
}

What can be inserted in the above code?

Select 2 option(s):
21.

Question:

Identify correct usages of the following annotation:

public @interface AuthorInfo {
    String name() default "";
}


Select 3 option(s):
22.

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

Collection<Number> col = new HashSet<>();
col.add(1);
var list1 = List.of(col); //1
col.add(2); //2
var list2 = List.copyOf(col); //3
System.out.println(list1+", "+list2);


Select 1 option(s):
23.

Question:

Consider that str is a local variable of class java.lang.String.
Which of the following lines of code may throw a NullPointerException in certain situations?

Or a tougher version of the question could be :
Which of the following lines of code are not an example of robust design ?

Select 3 option(s):
24.

Question:

Consider the following code:

interface Classic {
    int version = 1;
    public void read() ;
}

class MediaReader implements Classic{
    int version = 2;
    public void read() {

        //Insert code here

    }
}

public class ReaderTest{
    public static void main(String[] args) {
        MediaReader mr = new MediaReader();
        mr.read();
    }
}


What can be inserted in the code above so that it will print 1 when run?

Select 1 option(s):
25.

Question: Which line(s) of code in the following program will cause compilation errors?

public class TestClass{
   var value = 0; //1
   public static void main(var args[]) //2
   {
      var secondArg = Integer.parseInt(args[2]); //3
      if( true == 2 > 10 ) //4  
      {
         value = -10;
      }
      else{
         value =  secondArg;
      }
      for( ; value>0; value--) System.out.println("A"); //5
   }
}

Select 3 option(s):
26.

Question:

Given the following code, which method declarations can be inserted at line 1 without any problems?

public class OverloadTest{
public int sum(int i1, int i2) { return i1 + i2; }
// 1
}

Select 3 option(s):

27.

Question:

Consider the following code:

class Account{
    private String id; private double balance;

    ReentrantLock lock = new ReentrantLock();
    
    public void withdraw(double amt){
        try{
            lock.lock();
            if(balance > amt) balance = balance - amt;
        }finally{
            lock.unlock();
        }
    }
}

What can be done to make the above code thread safe?

Select 1 option(s):
28.

Question:

Given the following definitions and reference declarations:

interface I1 { }
interface I2 { }
class C1 implements I1 { }
class C2 implements I2 { }
class C3 extends C1 implements I2 { }
C1 o1;
C2 o2;
C3 o3;

Which of these statements are legal?

Select 3 option(s):
29.

Question: Which of the following statements regarding java.io.File are true?

Select 3 option(s):
30.

Question:

Identify correct statements about the following code -

interface Drink{
  default double getAlcoholPercent(){
     return 0.0;
  }
  
  static double computeAlcoholPercent(){
      return 0.0;
  }
}


interface ColdDrink extends Drink{
  String getName();
}

class CrazyDrink implements ColdDrink{
   // INSERT CODE HERE
}


Select 1 option(s):
31.

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. The following packages exist in these jars:

myapp.jar:   com.abc.myapp
datalayer.jar: com.abc.datalayer
mysql-connector-java-8.0.11.jar:  com.mysql.jdbc


You have decided to modularize your application even though datalayer and mysql libraries are still not modularlized. Which of the following would be a valid module-info for your app?

Select 1 option(s):
32.

Question: What letters will be printed by this program?

public class ForSwitch{
public static void main(String args[]){
char i;
LOOP: for (i=0;i<5;i++){
switch(i++){
case '0': System.out.println("A");
case 1: System.out.println("B"); break LOOP;
case 2: System.out.println("C"); break;
case 3: System.out.println("D"); break;
case 4: System.out.println("E");
case 'E' : System.out.println("F");
}
}
}
}

Select 2 option(s):

33.

Question:

How can you fix the following code to make it compile:

import java.io.*;
class Great {
    public void doStuff() throws FileNotFoundException{
    }    
}

class Amazing extends Great {
  public void doStuff() throws IOException, IllegalArgumentException{
  }    
}

public class TestClass {
    public static void main(String[] args) throws IOException{
        Great g = new Amazing();
        g.doStuff();
    }
}

Assume that changes suggested in a option are to be applied independently of changes suggested in other options.

Select 2 option(s):
34.

Question: What changes should be applied to the following class to update it to use generics without changing any functionality? Choose minimal changes that are necessary to take advantage of generics.

import java.util.*;
public class BookStore {
    Map map = new HashMap();    //1
    
    public BookStore(){
        map.put(new Book("A111"), new Integer(10)); //2
    }
    
    public int getNumberOfCopies(Book b){ //3
        Integer i = (Integer) map.get(b); //4
        return i == null? 0:i.intValue();  //5
    }
}

Select 2 option(s):
35.

Question:

Consider the following method exposed by a class of a third party library:

public static void storeObjectToFile(final String fn, final Serializable obj) throws Exception {
    validateOutputFile(fn);
    validatePermissions();
    try(var oos = new ObjectOutputStream(new FileOutputStream(fn)) ){
        oos.writeObject(obj);
    }
}


This method is used by several classes in your application to store user information in files. Identify correct statements about this approach.

Select 2 option(s):
36.

Question:

Your application consists of two jar files produced by two different teams - accounting-3.2.jar and reporting-5.6.jar. Classes in reporting-5.6.jar uses a class com.abc.account.Account in accounting-3.2.jar while classes in accounting-3.2.jar do not refer to classes from reporting-5.6.jar.

The accounting team has decided to modularize their jar. Which of the following would be a valid module-info.java for the new accounting-3.3.jar?

Select 1 option(s):
37.

Question:

Given:

1. Java source file location: src/moduleA/a/A.java
2. Expected class file location: out/moduleA/a/A.class

Which of the following commands will generate A.class in the expected directory?
(Assume that all directory paths are relative to the current directory.)

Select 1 option(s):
38.

Question: Which line(s) in the following code will fail compilation?

interface Tool{    //1
    void operate(); //2
}

abstract class PowerTool implements Tool{ } //3

class PowerSaw implements PowerTool{  //4
    @Override //5
    public void operate(){ }
}

class SteamPowerSaw extends PowerSaw{ //6
    @Override //7
    public void operate(int pressure){ }
}


Select 2 option(s):
39.

Question: Which statements about the following code are correct?

interface House{
  public default String getAddress(){
     return "101 Main Str";
  }
}

interface Office {
  public static String getAddress(){
     return "101 Smart Str";
  }
}

interface WFH extends House, Office{
   private boolean isOffice(){ return true; }
}

class HomeOffice implements House, Office{
  public String getAddress(){
     return "R No 1, Home";
  }
}

public class TestClass {

  public static void main(String[] args) {
    Office off = new HomeOffice();  //1
    System.out.println(off.getAddress()); //2
  }
}

Select 1 option(s):
40.

Question:

Identify the correct statements regarding DateFormat and NumberFormat classes.

Select 1 option(s):
41.

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

public class Paper {
    public String title;
    public int id;
    
    public Paper(String title, int id){
        this.title = title;
        this.id = id;
    }
    
    public static void main(String[] args) {
        var papers = new Paper[]{
            new Paper("T1", 1),
            new Paper("T2", 2),
            new Paper("T3", 3)
        };
        
        System.out.println(papers);
        System.out.println(papers[1]);
        System.out.println(papers[1].id);
    }
}

Select 1 option(s):
42.

Question: Which of the following statements are true?

Select 2 option(s):
43.

Question:

Given:

@Target(ElementType.FIELD)
public @interface DBField {
  public String type();
  public String value() default "";
  public int invalue() default 0;
}

Identify correct usages of the above annotation.

Select 1 option(s):
44.

Question:

Consider the following code:

class A{
public XXX m1(int a){
return a*10/4-30;
}
}
class A2 extends A{
public YYY m1(int a){
return a*10/4.0;
}
}

What can be substituted for XXX and YYY so that it can compile without any problems?

Select 1 option(s):
45.

Question: What will the following program print?

public class TestClass{
  static String str = "Hello World";
  public static void changeIt(String s){
    s = "Good bye world";
  }
  public static void main(String[] args){
    changeIt(str);
    System.out.println(str);
  }
}

Select 1 option(s):
46.

Question:

Given:


  byte b = 1;
  char c = 1;
  short s = 1;
  int i = 1;


which of the following expressions are valid?

Select 3 option(s):
47.

Question: Which of the following is a valid module-info for a service user module that uses an Order service defined in OrderServiceAPI module and implemented by OrderServiceProvider module?

Select 1 option(s):
48.

Question:

Consider the following code snippet:

    for(var i=INT1; i<INT2; i++){
        System.out.println(i);
    }


INT1 and INT2 can be any two integers.

Which of the following will produce the same result?

Select 1 option(s):
49.

Question:

Identify correct statement(s) about the following code:

public class FunWithOptional{
    public static String getValue(){
        return null;
    }
    public static void main(String[] args) {
        Optional<String> stro = Optional.of(getValue());//1
        System.out.println(stro.isPresent());//2
        System.out.println(stro.get());//3
        System.out.println(stro.orElse(null));//4
    }
}


Select 1 option(s):
50.

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

class TestClass implements I1, I2{
public void m1() { System.out.println("Hello"); }
public static void main(String[] args){
TestClass tc = new TestClass();
( (I1) tc).m1();
}
}
interface I1{
int VALUE = 1;
void m1();
}
interface I2{
int VALUE = 2;
void m1();
}

Select 1 option(s):

51.

Question: Which of the following statements are true regarding the try-with-resources statement?

Select 1 option(s):
52.

Question:

Given the following code fragment:

        List<String> cities = List.of("USA", "Netherlands", "UK", "India", "France");
        Comparator<String> c = (a, b)->a.compareTo(b); //1
        Comparator<String> cr = c.reversed();//2
        String joined = cities.stream().sorted(cr).collect(Collectors.joining(", "));//3
        System.out.println(joined);

What is the result?

Select 1 option(s):
53.

Question: 

Identify correct statements about the modular system of Java.

Select 3 option(s):
54.

Question:

Given:
        Path p1 = Paths.get("c:\\temp\\test1.txt");
        Path p2  = Paths.get("c:\\temp\\test2.txt");


Which of the following code fragments moves the file test1.txt to test2.txt, even if test2.txt exists?

Select 2 option(s):
55.

Question:

Consider the following code snippet:

public class Test{
  void test(){
      MyClass obj = new MyClass();
      obj.name = "jack";
      // 1 insert code here
  }
}

//In MyClass.java
public class MyClass{
  int value;
  String name;
}


What can be inserted at // 1, which will make the object referred to by obj eligible for garbage collection?

Select 1 option(s):