Welcome to your Test - 16 Code - 819

Name
Email
Phone
1.

Question:

Identify the correct statements about ArrayList?

Select 3 option(s):
2.

Question: When a class whose members should be accessible only to members of that class is coded such a way that its members are accessible to other classes as well, this is called:-

Select 1 option(s):
3.

Question: Which statements, when inserted in the code below, will cause an exception at run time?

class B {}
class B1 extends B {}
class B2 extends B {}
public class ExtendsTest{
public static void main(String args[]){
B b = new B();
B1 b1 = new B1();
B2 b2 = new B2();
// insert statement here
}
}

Select 1 option(s):

4.

Question:

Given the following RDBMS table information :

  STUDENT Table
      SID INT Primary Key
      NAME VARCHAR(50)
      GPA INT


and the following code:


        Statement stmt = connection.createStatement();
        ResultSet rs = stmt.executeQuery("select SID, NAME,  GPA from STUDENT");
        while(rs.next()){
            System.out.println(  INSERT CODE HERE );
        }
        connection.close();


What can be inserted in the above code so that it will print the GPA value for each student?

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

Select 2 option(s):
5.

Question:

Given:
var nums = IntStream.range(1, 5);

Which of the following options will compute the average of the numbers in the nums stream?

Select 3 option(s):
6.

Question:

Consider the following method:

public void getLocks(Object a, Object b)
{
synchronized(a) {
synchronized(b)
{
//do something
}
}
}

and the following instantiations:

Object obj1 = new Object();
Object obj2 = new Object();

obj1 and obj2 are accessible to two different threads and the threads are about to call the getLocks() method. Assume the first thread calls the method getLocks(obj1, obj2). Which of the following options avoids a deadlock?

Select 1 option(s):
7.

Question:

Given:

abstract class Vehicle{ }
interface Drivable{ }
class Car extends Vehicle implements Drivable{ }
class SUV extends Car { }

Which of the following options will compile?

Select 2 option(s):
8.

Question: What will the following code print when run?

public class Test{
static String j = "";
public static void method( int i){
  try{
   if(i == 2){
     throw new Exception();
   }
   j += "1";
  }
  catch (Exception e){
   j += "2";
   return;
  }
  finally{
   j += "3";
  }
  j += "4";
}
public static void main(String args[]){
  method(1);
  method(2);
  System.out.println(j);
}
}

Select 1 option(s):
9.

Question:

Your finance application invokes a third party analytics libray that takes a list of Bond objects and generates some results. Something like this:

Bond[] bondArray = //prepare an array of Bonds
Report r = BondProcessor.getInstance().analyzeBonds(bondsArray);


Which of the following statements are correct regarding above code from secure coding guidelines perspective?

Select 1 option(s):
10.

Question:

Consider the following method...

public int setVar(int a, int b, float c) { ...}

Which of the following methods correctly overload the above method?

Select 2 option(s):
11.

Question: Which of the following code segments will correctly write the text FINAL TEXT at the end of a file file.txt ?

Select 1 option(s):
12.

Question: Which of the lines will cause a compile time error in the following program?

public class MyClass{
public static void main(String args[]){
char c;
int i;
c = 'a';//1
i = c; //2
i++; //3
c = i; //4
c++; //5
}
}

Select 1 option(s):

13.

Question: What will the following code print?

List<Integer> names = Arrays.asList(1, 2, 3); //1
names.forEach(x->x=x+1); //2
names.forEach(System.out::println); //3

Select 1 option(s):
14.

Question:

Given the following code:

public  class Valuator {
    public AtomicInteger status = new AtomicInteger(0);
    public void valuate() {
        int oldstatus = status.get();
        /* valid code here */
        int newstatus = //determine new status

        //INSERT CODE HERE
        
    }
}

Assuming that an instance of this class is shared among multiple threads, you want to update the status to newstatus only if the oldstatus has not changed. Which of the following lines of code will you use?

Select 1 option(s):
15.

Question: Which of the following is a valid module-info for a service provider that provides an Order service defined in OrderServiceAPI module?

Select 1 option(s):
16.

Question:

Identify the correct statements about the following code:-


List<Integer> values = Arrays.asList(2, 4, 6, 9); //1
Predicate<Integer> check = (Integer i) -> {
    System.out.println("Checking");
    return i == 4; //2
};
Predicate even = (Integer i)-> i%2==0;  //3
values.stream().filter(check).filter(even).count(); //4


Select 1 option(s):
17.

Question:

Consider the following code:

import java.io.*;
public class TestClass {
    
   public static void main(String[] args) throws Exception {
      var f = new File("x");   //1
      var bfr1 = new BufferedReader(new FileReader(f)); //2
      var bfr2 = new BufferedReader( bfr1 ); //3
      var pw = new PrintWriter(new FileReader(f)); //4
   }
}


Select the correct statements about the above program.

Select 1 option(s):
18.

Question:

Consider the following code.

import java.time.*;
import java.time.format.*;
public class TestClass
{
    public static void main(String[] args) throws Exception
    {
        LocalDate d = LocalDate.now();
        //INSERT CODE HERE
        System.out.println(s);
    }
}


What should be inserted in the code above so that it will print the date in the following format:

Saturday 1st day of January 2000

Select 1 option(s):
19.

Question:

Given the following code:

Locale.setDefault(Locale.ITALY);
Locale loc = new Locale.Builder().setLanguage("en").build();
ResourceBundle rb = ResourceBundle.getBundle("mymsgs", loc);
System.out.println(rb.getString("helloMsg"));


and the following properties files:
mymsgs.properties
mymsgs_en_IT.properties
mymsgs_en.properties
mymsgs_IT.properties


Which of the following statements are correct?

Select 2 option(s):
20.

Question:

Given the following class definitions :

interface MyIface{};
class A {};
class B extends A implements MyIface{};
class C implements MyIface{};

and the following object instantiations:

A a = new A();
B b = new B();
C c = new C();

Which of the following assignments are legal at compile time?

Select 1 option(s):
21.

Question: Which jdeps option(s) is/are used to include dependent nonmodular jar files?

Note: We have seen some really vague and ill-designed questions without complete information on jdeps in the exam. It is anybody's guess as to what exactly are they testing.

Based on the given options, our guess is that in this question they are just checking whether you know that -cp, -classpath, and --class-path are equivalent options and the nonmodular jars should be put on classpath instead of module-path.

Select 3 option(s):
22.

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

        int[][] ab = { {1, 2, 3}, {4, 5} };
        for(var i=0; i<ab.length; i++){
            for(var j=0; j<ab[i].length; j++){
                System.out.print(ab[i][j]+" ");
                if(ab[i][j] == 2){
                    break;
                }
            }
            continue;
        }

Select 1 option(s):
23.

Question:

Given:

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

What is the result?

Select 1 option(s):
24.

Question:

The following program will print
java.lang.ArithmeticException: / by zero
class Test{
public static void main(String[] args){
int d = 0;
try{ int i = 1 / (d* doIt());
} catch (Exception e){
System.out.println(e);
}
}
public static int doIt() throws Exception{
throw new Exception("Forget It");
}
}

Select 1 option(s):
25.

Question:

Consider the following two classes (in the same package but defined in different source files):

public class Square {
double side = 0;
double area;
public Square(double length){ this.side = length; }
public double getSide() { return side; }
public void setSide(double side) { this.side = side; }
double getArea() { return area; }
}
public class TestClass {
public static void main(String[] args) throws Exception {
Square sq = new Square(10.0);
sq.area = sq.getSide()*sq.getSide();
System.out.println(sq.getArea());
}
}

You are assigned the task of refactoring the Square class to make it better in terms of encapsulation. What changes will you make to this class?

Select 4 option(s):
26.

Question: Which of the options will create and initialize a matrix of ints as shown below?

 1  
 1  
 1  
 1  
 1  

Select 1 option(s):
27.

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

public class SM{
   public String checkIt(String s){
      if(s.length() == 0 || s == null){
         return "EMPTY";
      }
      else return "NOT EMPTY";
    }
    
    public static void main(String[] args){
      SM a = new SM();
      System.out.println(a.checkIt(null));
    }
}

Select 1 option(s):
28.

Question:

Given:

import java.util.*;
public class TestClass {
public static void main(String[] args) throws Exception {
ArrayList<Double> al = new ArrayList<>();
//INSERT CODE HERE
}
}

What can be inserted in the above code so that it can compile without any error?

Select 2 option(s):
29.

Question:

Given:

String sentence1 = "Carpe diem. Seize the day, boys. Make your lives extraordinary.";
String sentence2 = "Frankly, my dear, I don't give a damn!";
String sentence3 = "Do I look like I give a damn?";
List sentences = Arrays.asList(sentence1, sentence2, sentence3);


Which of the following options will create a stream containing all the words in the three sentences without repetition?

Select 1 option(s):
30.

Question:

Consider the following program:

import java.util.*;
public class TestClass
{    
    static String[] sa = { "a", "aa", "aaa", "aaaa" };
    static
    {
        Arrays.sort(sa);
    }
    public static void main(String[] args)
    {
        String search = "";
        if(args.length != 0) search = args[0];
        System.out.println(Arrays.binarySearch(sa, search));
    }
}


What are all possible values this program can print when run?

Select 1 option(s):
31.

Question:

Given the following lines of code:

   int rate = 10;
   XXX amount = 1 - rate/100*1 - rate/100;


What can XXX be?

Select 1 option(s):
32.

Question: What will be the output when the following program is run?

public class TestClass{
public static void main(String args[]){
int i;
int j;
for (i = 0, j = 0; j < i; ++j, i++){
System.out.println(i + " " + j);
}
System.out.println(i + " " + j);
}
}

Select 1 option(s):

33.

Question: What will the following code print?

   Path p1 = Paths.get("c:\\temp\\test.txt");
   Path p2 = Paths.get("report.pdf");
   System.out.println(p1.resolve(p2));

Select 1 option(s):
34.

Question:

Given that Book is a valid class with appropriate constructor and getTitle and getPrice methods that return a String and a Double respectively, what will the following code print?

List<Book> books = Arrays.asList(
        new Book("Gone with the wind", 5.0),
        new Book("Gone with the wind", 10.0),
        new Book("Atlas Shrugged", 15.0)
);
books.stream().collect(Collectors.toMap((b->b.getTitle()), b->b.getPrice()))
                        .forEach((a, b)->System.out.println(a+" "+b));


Select 1 option(s):
35.

Question:

Following is a supposedly robust method to parse an input for a float :

public float parseFloat(String s){
float f = 0.0f;
try{
f = Float.valueOf(s).floatValue();
return f ;
}
catch(NumberFormatException nfe){
System.out.println("Invalid input " + s);
f = Float.NaN ;
return f;
}
finally { System.out.println("finally"); }
return f ;
}

Which of the following statements about the above method is/are true?

Select 1 option(s):
36.

Question: Which of the following can be valid declarations of an integer variable?

Select 2 option(s):
37.

Question:

The following are complete contents of ConsoleTest.java:

import java.io.Console;
public class ConsoleTest {
    public static void main(String[] args) {
        var c = System.console(); //1
        char[] line = c.readPassword("Please enter your pwd:"); //2
        System.out.println("Pwd is "+new String(line));         
    }
}

What will happen when it is compiled and run from the command line?

Select 1 option(s):
38.

Question: What will the following code print?

int i = 1;
int j = i++;
if( (i==++j) | (i++ == j) ){
i+=j;
}
System.out.println(i);

Select 1 option(s):

39.

Question: What will the following program print when run?

public class ChangeTest {
private int myValue = 0;
public void showOne(int myValue){
myValue = myValue;
}
public void showTwo(int myValue){
this.myValue = myValue;
}
public static void main(String[] args) {
var ct = new ChangeTest();
ct.showOne(100);
System.out.println(ct.myValue);
ct.showTwo(200);
System.out.println(ct.myValue);
}
}

Select 1 option(s):

40.

Question: Which of these array declarations and initializations are legal?

Select 1 option(s):
41.

Question:

Given:

import java.util.*;
class Data{
    int value;
    public Data(int x){ this.value = x; }
    public String toString(){ return ""+value; }
}

class MyFilter {
  public boolean test(Data d){
     return d.value == 0;
  }
}

public class TestClass{
    
   public static void filterData(ArrayList<Data> dataList, MyFilter f){
      Iterator<Data> i = dataList.iterator();
      while(i.hasNext()){
           if(f.test(i.next())){
                i.remove();
           }
       }
   }

  public static void main(String[] args) {
        ArrayList<Data> al = new ArrayList<Data>();
        Data d = new Data(1); al.add(d);
        d = new Data(2); al.add(d);
        d = new Data(0); al.add(d);

        filterData(al, new MyFilter());  //1

        System.out.println(al);
    }
}


How can you use a lambda expression to achieve the same result?

Select 1 option(s):
42.

Question:

Given:

List<Integer> primes = Arrays.asList(2, 3, 5, 7, 11, 13, 17); //1
Stream<Integer> primeStream = primes.stream(); //2

Predicate<Integer> test1 = k->k<10; //3
long count1 = primeStream.filter(test1).count();//4

Predicate<Integer> test2 = k->k>10; //5
long count2 = primeStream.filter(test2).count(); //6

System.out.println(count1+" "+count2); //7


Identify correct statements.

Select 1 option(s):
43.

Question: What will the following program print?

public class TestClass{
public static void main(String[] args){
for : for(var i = 0; i< 10; i++){
for (var j = 0; j< 10; j++){
if ( i+ j > 10 ) break for;
}
System.out.println( "hello");

}
}
}

Select 1 option(s):

44.

Question:

Consider the following method...

public void ifTest(boolean flag){
   if (flag)   //1
   if (flag)   //2
   System.out.println("True False");
   else        // 3
   System.out.println("True True");
   else        // 4
   System.out.println("False False");
}


Which of the following statements are correct ?

Select 3 option(s):
45.

Question:

Given:

var bfr = new BufferedReader(new FileReader("c:\\temp\\pathtest\\a.java"));
var bfw = new BufferedWriter(new FileWriter("c:\\temp\\pathtest\\b.java"));
String line = null;
while( (line=bfr.readLine()) != null ){
    bfw.append(line);
}

//INSERT CODE HERE

bfw.close();


Which of the following lines is required to be inserted in the code above so that content in b.java will be overwritten with the content in a.java?

Select 1 option(s):
46.

Question: What will the following program print?

class Test{
public static void main(String args[]){
int i=0, j=0;
X1: for(i = 0; i < 3; i++){
X2: for(j = 3; j > 0; j--){
if(i < j) continue X1;
else break X2;
}
}
System.out.println(i+" "+j);
}
}

Select 1 option(s):

47.

Question:

The developer of a method that accesses the file system wants to ensure that the caller has appropriate permissions. She has written the following code:


public class FileOps {
     public static void doOps() {
                return AccessController.doPrivileged(
                    new PrivilegedAction<String>() {
                        public String run() {
                            //do File operations here
                        }
                    }, AccessController.getContext()
                );
            }
}


What, if anything, is wrong with this code from a security perspective?

Select 1 option(s):
48.

Question:

Consider the following code:

import java.util.*;
class Book{ }
class TextBook extends Book{ }

class BookList extends ArrayList<Book>
{
    public int count = 0;
    public boolean add(Object o)
    {
       if(o instanceof Book ) return super.add((Book) o);
       else return count++ == -1;
    }
}

//in valid context
BookList list = new BookList();
list.add(new Book());
list.add(new TextBook());
list.add("hello");

System.out.println(list.count);


What will it print?

Select 1 option(s):
49.

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

class A {
public int getCode(){ return 2;}
}
class AA extends A {
public long getCode(){ return 3;}
}
public class TestClass
{
public static void main(String[] args) throws Exception {
A a = new A();
A aa = new AA();
System.out.println(a.getCode()+" "+aa.getCode());
}
public int getCode() {
return 1;
}
}

Select 1 option(s):

50.

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

public class Nesting
{
    public static void main(String args[])
    {
        B.C obj = new B( ).new C( );
    }
}
class A
{
    char c;
    A(char c) { this.c = c; }
}
class B extends A
{
   char c = 'a';
   B( ) { super('b'); }
   class C extends A
   {
      char c = 'c';
      C( )
      {
         super('d');
         System.out.println(B.this.c);
         System.out.println(C.this.c);
         System.out.println(super.c);
      }
   }
}

Select 1 option(s):
51.

Question: Which of the statements regarding the given code are correct?

public class Test extends Thread
{
static Object obj1 = new Object();
static Object obj2 = new Object();
public void m1()
{
   synchronized(obj1)
   {
     System.out.print("1 ");
     synchronized(obj2)
     {
       System.out.println("2");
     }
   }
}
public void m2()
{
   synchronized(obj2)
   {
     System.out.print("2 ");
     synchronized(obj1)
     {
       System.out.println("1");
     }
   }
}
public void run()
{
   m1();
   m2();
}
public static void main(String[] args)
{
   new Test().start();
   new Test().start();
}
}

Select 1 option(s):
52.

Question:

Your application uses one modular jar (a.jar), which, in turn, uses one non-modular jar (b.jar). Which of the following commands will cause jdeps to include the non-modular jar in its analysis?

Select 1 option(s):
53.

Question:

Consider the following piece of code:

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

You have created two resource bundle files with the following contents:

#In appmessages.properties:
greetings=Hello

#In appmessages_fr_FR.properties:
greetings=bonjour

Given that this code is run on machines all over the world. Which of the following statements are correct?

Select 1 option(s):
54.

Question:

Assuming that the following code compiles without any error, identify correct statements.


interface Processor {
   A process(String str);
}

class ItemProcessor implements Processor{
   @Override
   public B process(String str){
       return new B(str);
   }
}


Select 2 option(s):
55.

Question: What will the following code print when run?

public class TestClass {

    public void switchString(String input){
        switch(input){
            case "a" : System.out.println( "apple" );
            case "b" : System.out.println( "bat" );
                break;
            case "c" : System.out.println( "cat" );                
            default : System.out.println( "none" );
        }
    }

    public static void main(String[] args) throws Exception {
        var tc = new TestClass();
        tc.switchString("c");
    }
}


Select 1 option(s):
56.

Question: Which of the following statements will evaluate to true?

Select 1 option(s):