Welcome to your Test - 8 Code - 819

Name
Email
Phone
1.

Question:

Consider the following code:

public class TestOuter
{
   public void myOuterMethod()
   {
      // 1
   }
   public class TestInner { }
   public static void main(String[] args)
   {
      var to = new TestOuter();
      // 2
   }
}


Which of the following options correctly instantiates a TestInner object?

Select 2 option(s):
2.

Question: 

A modularized application has been packaged in graphs.jar file with module name as com.xcomp.graphs. The entry point of the application is a class named Main in package com.xcomp.graphs.

Which two options can be used to execute this application?

Select 2 option(s):
3.

Question: What will be the output of the following program ?

class CorbaComponent{
String ior;
CorbaComponent(){ startUp("IOR"); }
void startUp(String s){ ior = s; }
void print(){ System.out.println(ior); }
}
class OrderManager extends CorbaComponent{
OrderManager(){ }
void startUp(String s){ ior = getIORFromURL(s); }
String getIORFromURL(String s){ return "URL://"+s; }
}
public class Application{
public static void main(String args[]){ start(new OrderManager()); }
static void start(CorbaComponent cc){ cc.print(); }
}

Select 1 option(s):

4.

Question: What will the following code print when run?

import java.util.*;
import java.text.*;

public class TestClass
{
    public static void main(String[] args)
    {
        double amount = 123456.789;
        Locale fr = new Locale("fr", "FR");
        NumberFormat formatter = NumberFormat.getInstance(fr);
        String s = formatter.format(amount) ;
        formatter = NumberFormat.getInstance();
        Number amount2 = formatter.parse(s);
        System.out.println( amount + " " + amount2 );
    }
}


Select 1 option(s):
5.

Question:

Given:

public class UserCredential implements Serializable{
  private String useridpwd;
  public UserCredential(String useridpwd){
     this.useridpwd = useridpwd;
  }
}


Which options can be used to secure serialization of the objects of the above class?

Note: This is a vague question because it is not clear exactly what is expected when it says "secure serialization of the objects". Does it mean the useridpwd must not be stored in the serialized data, does it mean UserCredential object must not be serialized, or does it mean the useridpwd field must not be tampered with or something else.

We have seen similar question on the exam. You should go through Oracle's secure coding guideline article before answering this question: 

Select 4 option(s):
6.

Question:

Given:

public class SimpleLoop {
    public static void main(String[] args) {
        int i=0, j=10;
        var count = 0;
        while (i<j) {            
            i++;
            j--;
            count++;
        }
        System.out.println(i+" "+j+" "+count);
    }
}

What is the result?

Select 1 option(s):
7.

Question: Which of the following are true regarding overloading of a method?

Select 1 option(s):
8.

Question:

Given:

class Person{
    private String name;
    private int age;
    public Person(String name, int age){
        this.name = name;
        this.age = age;
    }
    //getters/setters not shown
}

What will the following code print?

List<Person> friends = Arrays.asList(new Person("Bob", 31),
                                 new Person("Paul", 32),
                                 new Person("John", 33));
double averageAge = friends.stream().filter(f->f.getAge()<30)
        .mapToInt(f->f.getAge())
        .average().getAsDouble();
System.out.println(averageAge);


Select 1 option(s):
9.

Question: Which of these are NOT legal declarations within a class?

Select 1 option(s):
10.

Question:

Consider the following program:

import java.io.FileReader;
import java.io.FileWriter;

public class ClosingTest {
    public static void main(String[] args) throws Exception {
        try(FileReader fr = new FileReader("c:\\temp\\license.txt");
            FileWriter fw = new FileWriter("c:\\temp\\license2.txt") )
        {
            int x = -1;
            while( (x = fr.read()) != -1){
                fw.write(x);
            }
        }
    }
}


Identify the correct statements.

Select 1 option(s):
11.

Question: What will the following program print?

class Test{
   public static void main(String args[]){
      var k = 9;
      var s = 5;
      switch(k){
         default :
         if( k == 10) { s = s*2; }
         else{
            s = s+4;
            break;
         }
         case 7 : s = s+3;
      }
      System.out.println(s);
   }
}


Select 1 option(s):
12.

Question:

The following code snippet will print 4.

int i1 = 1, i2 = 2, i3 = 3;
int i4 = i1 + (i2=i3 );
System.out.println(i4);

Select 1 option(s):

13.

Question: Which of the following code snippet will print 10 random double values?

Note: Although not mentioned in the exam objectives, we have seen questions that require knowledge about java.lang.Random class on the exam.

Select 3 option(s):
14.

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

public class SortTest {
    public static void main(String[] args) {
        Object[] sa = { 100, 100.0, "100" };
        Collections.sort(Arrays.asList(sa), null);
        System.out.println(sa[0]+" "+sa[1]+" "+sa[2] );
    }

}

Select 1 option(s):
15.

Question:

Given:

int[][] orig = { { 1, 2, 3} , { 4, 5, 6, 7}};
int[][] dup = orig.clone();
int[] copy = dup[0].clone();

System.out.println(orig == dup);
System.out.println(orig.equals(dup));
System.out.println(orig[0] == dup[0]);
System.out.println(dup[0] == copy);
System.out.println(dup[0].equals(copy));


How many times will true be printed?

Select 1 option(s):
16.

Question:

Given:

module broker{
  exports org.broker.api;
  provides org.broker.api.Broker with org.broker.api.MyBroker
}


Which of the following statements are correct?

Select 3 option(s):
17.

Question: What would be the result of trying to compile and run the following program?

public class Test{
   int[] ia = new int[1];
   Object oA[]  = new Object[1];
   boolean bool;
   public static void main(String args[]){
      var test = new Test();
      System.out.println(test.ia[0] + "  " + test.oA[0]+"  "+test.bool);
   }
}

Select 1 option(s):
18.

Question:

Consider the following interface definition:

interface Measurement{
    public default int getLength(){
        System.out.println("getting length");
        return 10;
    };
    public default int getBreadth(){
        System.out.println("getting length");
        return 20;
    };
}

Several similar default methods need to be added to this interface and each method is supposed to log a message at the start of the method. As of now, the logging is done using just a println statement but it may change later.

What changes can be done in the above interface to make it more maintainable without exposing any non-business functionality to the users of this interface?

Select 1 option(s):
19.

Question What will the following code print when run?

    String[] sa = { "charlie", "bob", "andy", "dave" };
    Collections.sort(Arrays.asList(sa), null);
    System.out.println(sa[0]);


Select 1 option(s):
20.

Question: What will the following code NEVER print when run?

enum SIZE
{
    TALL, JUMBO, GRANDE;
}

class CoffeeMug
{

    public static void main(String[] args)
    {
        var hs = new HashSet<SIZE>();
        hs.add(SIZE.TALL); hs.add(SIZE.JUMBO); hs.add(SIZE.GRANDE);
        hs.add(SIZE.TALL); hs.add(SIZE.TALL); hs.add(SIZE.JUMBO);
        for(SIZE s : hs) System.out.print(s+" ");
    }
}

Select 2 option(s):
21.

Question: What can be inserted in the following code so that it will print 12?

public class TestClass{
    interface Doer{
        String doIt(int x, String y);
   }
   public static void main(String[] args) {

        INSERT CODE HERE

        System.out.println(d.doIt(2, "12345"));
    }
}

Select 2 option(s):
22.

Question: When is the Object created at line //1 eligible for garbage collection?

public class TestClass{
  public Object getObject(){
     Object obj = new String("aaaaa");   //1
     Object objArr[] = new Object[1]; //2
     objArr[0] = obj; //3
     obj = null;      //4
     objArr[0] = null;//5
     return obj;      //6
  }
}

Select 1 option(s):
23.

Question:

Given:

public class Student {
    private String name;
    private int marks;

    //constructor and getters and setters not shown

    public void addMarks(int m){
        this.marks += m;
    }
    public void debug(){
        System.out.println(name+":"+marks);
    }
}

What will the following code print when compiled and run?

List<Student> slist = List.of(new Student("S1", 40), new Student("S2", 35), new Student("S3", 30));
Consumer<Student> increaseMarks = s->s.addMarks(10);
slist.forEach(increaseMarks);
slist.forEach(Student::debug);


Select 1 option(s):
24.

Question: java.util.Locale allows you to do which of the following?

Select 2 option(s):
25.

Question: What will the following code fragment print?

        Path p1 = Paths.get("x\\y");
        Path p2 = Paths.get("z");
        Path p3 = p1.relativize(p2);
        System.out.println(p3);

Select 1 option(s):
26.

Question: What is the result of executing the following fragment of code:

boolean b1 = false;
boolean b2 = false;
if (b2 != b1 = !b2){
   System.out.println("true");
}
else{
   System.out.println("false");
}

Select 1 option(s):
27.

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

public class Test{
    static int a = 0;
    int b = 5;

    public void foo(){
        while(b>0){
            b--;
            a++;
        }
    }

    public static void main(String[] args) {
        var p1 = new Test();
        p1.foo();
        var p2 = new Test();
        p2.foo();

        System.out.println(p1.a+" "+p2.a);        
    }
}

Select 1 option(s):
28.

Question:

Consider the following code:

class A
{
   byte getStatusCode(Object obj) throws NullPointerException
   {
      if(obj != null ) return 128;
      else return -1;
   }
}
class B extends A
{
    //override getStatusCode method.
}

Which of the following statements are valid?

Select 3 option(s):
29.

Question:

Consider the following code appearing in Eagle.java

class Bird {
private Bird(){ }
}
class Eagle extends Bird {
public String name;
public Eagle(String name){
this.name = name;
}
public static void main(String[] args) {
System.out.println(new Eagle("Bald Eagle").name);
}
}

What can be done to make this code compile?

Select 1 option(s):
30.

Question:

Given:

public class Triangle{
public int base;
public int height;
public double area;
public Triangle(int base, int height){
this.base = base; this.height = height;
updateArea();
}
void updateArea(){
area = base*height/2;
}
public void setBase(int b){ base = b; updateArea(); }
public void setHeight(int h){ height = h; updateArea(); }
}

The above class needs to protect an invariant on the "area" field. Which three members must have the public access modifiers removed to ensure that the invariant is maintained?

Select 3 option(s):
31.

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

public class TestClass {
    
    public static void main(String[] args){
        var k = 2;
        while(--k){
            System.out.println(k);
        }
    }
}

Select 1 option(s):
32.

Question: What will the following code print when run?

class A {
}
class AA extends A {
}
public class TestClass {
public static void main(String[] args) throws Exception {
A a = new A();
AA aa = new AA();
a = aa;
System.out.println("a = "+a.getClass());
System.out.println("aa = "+aa.getClass());
}
}

Select 1 option(s):

33.

Question:

Consider the following code:

        String[] dataList = {"x", "y", "z"};
        for (var dataElement : dataList) {
            int innerCounter = 0;
            while (innerCounter < dataList.length) {
                System.out.println(dataElement + ", " + innerCounter);
                innerCounter++;
            }

        }

How many times will the output contain 2?

Select 1 option(s):
34.

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

BinaryOperator<String> bo = (s1, s2) -> s1.concat(s2);
List<String> names  = new ArrayList<>();
names.add("Bill"); names.add("George"); names.add("Obama");
String finalvalue = names.stream().reduce("Hello : ", bo);
System.out.println(finalvalue);

Select 1 option(s):
35.

Question:

Given:

class MyProcessor{
    int value;
    public MyProcessor(){ value = 10; }
    public MyProcessor(int value){
        this.value = value;
    }
    public void process(){
        System.out.println("Processing "+value);
    }
}

Which of the following code snippets will print Processing 10?

Select 2 option(s):
36.

Question: What will the following method return if called with an argument of 7?

public int transformNumber(int n){
   var radix = 2;
   var output = 0;
   output += radix*n;
   radix = output/radix;
   if(output<14){
       return output;
   }
   else{
       output = output*radix/2;
       return output;
   }
   else {
       return output/2;
   }
}

Select 1 option(s):
37.

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

Question:

You are working on an application that is NOT structured as a modular application. However, you need to use a third party library that is packaged as a modular jar named abc.utils.jar in your application.

Given that you launch your application using a class named Main, which of the following options should be used to make sure your application is able to access the classes in the third party jar?
(Assume that abc.utils.jar and your application classes are in current directory.)

Select 1 option(s):
39.

Question:

Consider the following program...

public class Outer
{
    private double d = 10.0;
    //put inner class here.
}

Which of the following options can be inserted in Outer class?

Select 3 option(s):
40.

Question:

Given the following class packaged in a.jar:

public class SecureClass1 {
     public static String getProp(final String propName) {
                return AccessController.doPrivileged(
                    new PrivilegedAction<String>() {
                        public String run() {
                            return System.getProperty(propName);
                        }
                    }, AccessController.getContext()
                );
            }
}


A developer has written the following class and packaged it in b.jar. To ensure that SecureClass2 is not able to access any system property, the developer has wrapped the call to doWork within the doPrivileged method and has passed in an AccessControlContext without any permissions.


public class SecureClass2 {
     
    private static final java.security.PermissionCollection noPermissions = new java.security.Permissions();
    private static final AccessControlContext noPermissionsAcc =
            new AccessControlContext(
                new ProtectionDomain[] {
                    new ProtectionDomain(null, noPermissions)
                }
            );     
    
     public static void doWork(final String propName){
        AccessController.doPrivileged( new PrivilegedAction<Void>() {
                public Void run() {
                    System.out.println("In SecureClass2 : "+SecureClass1.getProp(propName));
                    return null;
                }
            }, noPermissionsAcc);         
     }
     
    public static void main(String[] args) {
         SecureClass2.doWork(args[0]);
    }
}


Another developer executes SecureClass2 with a policy file containing the following permission:

grant codeBase "file:c:\\temp\\a.jar" {
    permission  java.util.PropertyPermission "java.home", "read";
};


Assuming that both the jars are in the classpath and that SecureClass2 is executed from the command line with a SecurityManager present, identify the correct statement(s).

Select 1 option(s):
41.

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

List<String> names = Arrays.asList("greg", "dave", "don", "ed", "fred" );
Map<Integer, Long> data = names.stream().collect(Collectors.groupingBy(
                             String::length,
                             Collectors.counting()) );
System.out.println(data.values());


Select 1 option(s):
42.

Question:

Consider the following code snippet:

//INSERT LINE OF CODE HERE
  switch( condition ){
     case 1  : System.out.println("1");   break;
     case 2  : System.out.println("2");   break;
     case 3 : System.out.println("3");  break;
  }


What type can be inserted in the code above so that the above code compiles and runs as expected ?

Select 2 option(s):
43.

Question:

Given the following module-info:

module book{
   requires org.pdf;
   uses org.pdf.Print;
}


Which of the following statements are correct?

Select 1 option(s):
44.

Question: Which line, if any, will give a compile time error ?

void test(byte x){
switch(x){
case 'a': // 1 c
ase 256: // 2
case 0: // 3
default : // 4
case 80: // 5
}
}

Select 1 option(s):

45.

Question:

Given:

module abc.print{
   requires org.pdf;
   provides org.pdf.Print with com.abc.print.SimplePrintImpl;
   provides org.pdf.Print with com.abc.print.ComplexPrintImpl;
}


Assuming that another module named book uses abc.print module and contains the following code,

   ServiceLoader<Print> psLoader = ServiceLoader.load(Print.class);
   for (Print p : psLoader){
       p.print("Hello");
   }

identify correct statements.

Select 1 option(s):
46.

Question: Which of these expressions will return true?

Select 4 option(s):
47.

Question: Which of the following are valid?

(Assume the method bodies are valid.)

Note: The topic of generics is not mentioned in exam objectives. However, we have seen candidates getting questions that expect basic idea of generics. It is possible that such questions are unscored (i.e. your answer to these questions are not considered while computing your final score.)

Select 3 option(s):
48.

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

Select 1 option(s):
49.

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

Question:

Given :

//1
public class TestClass {
//2
   public static void main(String[] args) throws Exception{
        var al = new ArrayList<Integer>();
        printElements(al);
   }

//3
    static void printElements(List<Integer>... la) {
       for(List<Integer> l : la){
           System.out.println(l);
       }
    }
}


Which option(s) will get rid of compilation warning(s) for the above code?

Select 1 option(s):
51.

Question:

Given:

module abc.blogger {
  requires serviceapi;
  provides api.BloggerService with abc.SimpleBlogger;
}


Which of the following code fragments appearing in another module correctly loads a service provider that implements api.BloggerService?

Select 1 option(s):
52.

Question: What will the following code print when run?

class Baap {
    public int h = 4;
    public int getH() {
        System.out.println("Baap " + h);
        return h;
    }
}

public class Beta extends Baap {
    public int h = 44;
    public int getH() {
        System.out.println("Beta " + h);
        return h;
    }
    public static void main(String[] args) {
        Baap b = new Beta();
        System.out.println(b.h + " " + b.getH());
        Beta bb = (Beta) b;
        System.out.println(bb.h + " " + bb.getH());
    }
}


Select 1 option(s):
53.

Question: What will the following code print?

List s1 = new ArrayList( );
s1.add("a");
s1.add("b");
s1.add("c");
s1.add("a");
System.out.println(s1.remove("a")+" "+s1.remove("x"));


Select 1 option(s):