Welcome to your Test - 14 Code - 819

Name
Email
Phone
1.

Question

NOTE: Although Class.isArray() and Object.clone()  methods are not officially on the exam, you may still see a question or two in the exam that requires you to know about these methods.

Given the following declaration:

  int[][] twoD = { { 1, 2, 3} , { 4, 5, 6, 7}, { 8, 9, 10 } };

What will the following lines of code print?

System.out.print(twoD[1].length);
System.out.print(twoD[2].getClass().isArray());
System.out.print(twoD[1][2]);
System.out.print(twoD[0] == twoD.clone()[0]);


Select 1 option(s):
2.

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

Select 2 option(s):
3.

Question: What will be printed by the following code if it is run with command line: java TestClass -0.50 ?

public class TestClass{
   public static double getSwitch(String str){
      return Double.parseDouble(str.substring(1, str.length()-1) );
   }
   public static void main(String args []){
         switch(getSwitch(args[0])){
            case 0.0 : System.out.println("Hello");
            case 1.0 : System.out.println("World"); break;
            default : System.out.println("Good Bye");
         }
   }
}

Select 1 option(s):
4.

Question:

Given the following method code:

public void myMethod() throws MyException {

  //method code

}


Select 1 option(s):
5.

Question: Which of the following are valid implementations of java.util.Comparator?

Select 3 option(s):
6.

Question: Which statements regarding the following code are correct?

class Outer
{
   private void Outer() { }
   protected class Inner
   {
   }
}

Select 1 option(s):
7.

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

class Test
{
   static int i1, i2, i3;
   public static void main(String[] args)
   {
      try
      {
         test(i1 = 1, oops(i2=2), i3 = 3);
      } catch (Exception e)
      {
         System.out.println(i1+" "+i2+" "+i3);
      }
   }
   static int oops(int i) throws Exception
   {
      throw new Exception("oops");
   }
   static int test(int a, int b, int c) {   
      return a + b + c;   
   }
}

Select 1 option(s):
8.

Question:

Given:

class Course{
    private String id;
    private String name;

    public Course(String id, String name){
        this.id = id; this.name = name;
    }
    //accessors not shows
}

What will the following code print?

List<Course> cList = Arrays.asList(
        new Course("803", "OCAJP 7"),
        new Course("808", "OCAJP 8"),
        new Course("809", "OCPJP 8")
);
            
cList.stream().filter(c->c.getName().indexOf("8")>-1)
        .map(c->c.getId())
        .collect(Collectors.joining("1Z0-"));
cList.stream().forEach(c->System.out.println(c.getId()));


Select 1 option(s):
9.

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

List<Integer> ls = Arrays.asList(1, 2, 3);
ls.stream().forEach(System.out::print)
   .map(a->a*2)
   .forEach(System.out::print);

Select 1 option(s):
10.

Question: Which of the following statements are true?

Select 2 option(s):
11.

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

public class LoadTest{
public static void main(String[] args) throws Exception {
LoadTest t = new LoadTest();
int i = t.getLoad();
double d = t.getLoad();
System.out.println( i + d );
}
public int getLoad() {
return 1;
}
public double getLoad(){
return 3.0;
}
}

Select 1 option(s):

12.

Question:

Given the following code:

class M { }
class N{
   private M m = new M();
   public void makeItNull(M pM){
      pM = null;
   }
   public void makeThisNull(){
      makeItNull(m);
   }
   public static void main(String[] args){
      N n = new N();
      n.makeThisNull();
   }
}


Which of the following statements are correct?

Select 1 option(s):
13.

Question:

Given:

public class Switcher{

   public static void main(String[] args){
       switch(Integer.parseInt(args[1]))  //1
       {
          case 0 :
             var b = false; //2
             break;
     
          case 1 :
             b = true; // 3
             break;
       }
       
       if(b) System.out.println(args[2]); //4
   }
}

What will the above  program print if compiled and run using the following command line:

java Switcher 1 2 3



Select 1 option(s):
14.

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

class TestClass{
   static TestClass ref;
   String[] arguments;
   public static void main(String args[]){
      ref = new TestClass();
      ref.func(args);
   }
   public void func(String[] args){
      ref.arguments = args;
   }
}

Select 1 option(s):
15.

Question:

Consider the following class hierarchy shown in the image. (B1 and B2 are subclasses of A and C1, C2 are subclasses of B1)

Assume that method public void m1(){ ... } is defined in all of these classes EXCEPT B1 and C1.

Assume that "objectOfXX" means a variable that points to an object of class XX. So, objectOfC1 means a reference variable that is pointing to an object of class C1.

Which of the following statements are correct?

Select 1 option(s):
16.

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

List<Integer> iList = Arrays.asList(1, 2, 3, 4, 5, 6, 7);
Predicate<Integer> p = x->x%2==0;
List newList = iList.stream().filter(p).filter(x->x>3).collect(Collectors.toList());
System.out.println(newList);

Select 1 option(s):
17.

Question:

Identify correct statement about the following code -

class X{
int val = 10;
}

class Y extends X{
Y val = null; //1
}

public class TestClass extends X{

  public static void main(String[] args){
    Y y = new Y();
    int k = (X) y.val ; //2
    System.out.println(k);

  }
}


Select 1 option(s):
18.

Question: What can be inserted in the following code at //1 so that it will print the number of lines present in the given file?

public void countLines(String filePath) throws Exception{
    //1
   System.out.println(ref.count());
}

Select 1 option(s):
19.

Question: Given the following declarations, identify which statements will return true:

Integer i1 = 1;
Integer i2 = new Integer(1);
int i3 = 1;
Byte b1 = 1;
Long g1 = 1L;
Double d = 1.0;

Select 2 option(s):

20.

Question: What will the following program print?

public class TestClass{
  public static void main(String[] args){
     int x = 1;
     int y = 0;
     if( x/y ) System.out.println("Good");
     else  System.out.println("Bad");
  }
}

Select 1 option(s):
21.

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

class Base{
void methodA(){
System.out.println("base - MethodA");
}
}
class Sub extends Base{
public void methodA(){
System.out.println("sub - MethodA");
}
public void methodB(){
System.out.println("sub - MethodB");
}
public static void main(String args[]){
Base b=new Sub(); //1
b.methodA(); //2
b.methodB(); //3
}
}

Select 1 option(s):

22.

Question: Which of the following code snippets will compile without any errors?

(Assume that the statement int x = 0; exists prior to the statements below.)

Select 3 option(s):
23.

Question:

Given:

public static void createFile(String name) throws Exception{
    try (
            OutputStream os = new FileOutputStream(name);  ) {

        //INSERT CODE HERE       

        //flush and close the streams that are open
    }
    
}


Which of the following combinations of the lines of code and their outcome when inserted above, are correct?

Select 2 option(s):
24.

Question:

You are creating a abc.payroll module that makes its two classes -  com.abc.payroll.Hourly and com.abc.payroll.Yearly - available to all other modules for use.

Which of the following files correctly defines this module?

Select 1 option(s):
25.

Question: What will the following program print?

public class TestClass{
static int someInt = 10;
public static void changeIt(int a){
a = 20;
}
public static void main(String[] args){
changeIt(someInt);
System.out.println(someInt);
}
}

Select 1 option(s):

26.

Question:

Given:

public class Course{
    private String id;
    private String category;

    public Course(String id, String category){
        this.id = id; this.category = category;
    }
    
    public String toString(){
        return id+" "+category;
    }

    //accessors not shown
}

What will the following code print?

List<Course> s1 = Arrays.asList(
        new Course("OCAJP", "Java"),
        new Course("OCPJP", "Java"),
        new Course("C#", "C#"),
        new Course("OCEJPA", "Java")
);

s1.stream()
       .collect(Collectors.groupingBy(c->c.getCategory()))
       .forEach((m, n)->System.out.println(n));

Select 1 option(s):
27.

Question:

Given the following classes, what will be the output of compiling and running the class Truck?

class Automobile{
public void drive() { System.out.println("Automobile: drive"); }
}
public class Truck extends Automobile{
public void drive() { System.out.println("Truck: drive"); }
public static void main (String args [ ]){
Automobile a = new Automobile();
Truck t = new Truck();
a.drive(); //1
t.drive(); //2
a = t; //3
a.drive(); //4
}
}

//End of Code

Select 1 option(s):
28.

Question: What will the following code print?

List s1 = new ArrayList();
String s = null;
s1.add("a");
s1.add(s); //1
s1.add("b");
s1.remove(s); //2
System.out.println(s1); //3

Select 1 option(s):
29.

Question:

Consider the following code:

public class FileCopier {
    public static void copy(String records1, String records2)  {
        try {
            InputStream is = new FileInputStream(records1);
            OutputStream os = new FileOutputStream(records2);
            byte[] buffer = new byte[1024];
            int bytesRead = 0;
            while ((bytesRead = is.read(buffer)) != -1) {
                os.write(buffer, 0, bytesRead);
                System.out.println("Read and written bytes " + bytesRead);
            }
        } catch (FileNotFoundException | IndexOutOfBoundsException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        copy("c:\\temp\\test1.txt", "c:\\temp\\test2.txt");
    }
}


Assuming appropriate import statements and the existence of both the files, what will happen when the program is compiled and run?

Select 1 option(s):
30.

Question: Which of the following method implementations will write a boolean value to the underlying stream?

Select 3 option(s):
31.

Question:

Consider the following code written to generate a report containing customer data where each line is a pipe separated list of values.

        
        String qr = "select EMAILID, NAME,  PHONE from CUSTOMER order by EMAILID";
        PreparedStatement stmt = connection.prepareStatement(qr);
        ResultSet rs = stmt.executeQuery(); //LINE 10
        while(rs.next()){
            System.out.println(rs.getString(0)+"|"+rs.getString(1)+"|"+rs.getString(3));
        }
        connection.close();


Identify the correct statement about this code.
(Assume that items not specified such as import statements and try/catch block are all valid.)

Select 1 option(s):
32.

Question:

The signature of a method in a class is as follows:

public static <E extends CharSequence> List<? super E> doIt(List<E> nums)

This method is being called in the following code:

result = doIt(in);

Given that String implements CharSequence interface, what should be the reference type of 'in' and 'result' variables?

Select 1 option(s):
33.

Question:

Given:

@Retention(RetentionPolicy.RUNTIME)
public @interface DebugInfo {
    String[] params() default {""};
    String date() default "";
    int depth() default 10;
    String value() ;
}

Which of the following options correctly uses the above annotation?

Select 3 option(s):
34.

Question:

Consider the following class hierarchy

class A{
public void m1() { }
}
class B extends A{
public void m1() { }
}
class C extends B{
public void m1(){
/* //1
... lot of code. */
}
}

Select 2 option(s):

35.

Question:

Consider the following code:

Statement stmt = null;
try(Connection c = DriverManager.getConnection("jdbc:derby://localhost:1527/sample", "app", "app"))
{
    stmt = c.createStatement();
    ResultSet rs = stmt.executeQuery("select * from STUDENT");
    while(rs.next()){
        System.out.println(rs.getString(1));
    }

}
catch(SQLException e){
    System.out.println("Exception "+e);
}


Which objects can be successfully used to query the database after the try block ends without any exception?

Select 1 option(s):
36.

Question:

Given:

public class Book{
    private int id;
    private String title;
//constructors and accessors not shown
}

Assuming that book is a reference to a valid Book object, which of the following code fragments correctly prints the details of the Book?

Select 1 option(s):
37.

Question: What will be the output when the following class is compiled and run?

class ScopeTest{
   static int x = 5;
   public static void main(String[] args){
      int x  = ( x=3 ) * 4;  // 1
      System.out.println(x);
   }
}

Select 1 option(s):
38.

Question:

Given that the file test.txt contains :

12345678


What will the following code print when compiled and run?

public static void main(String[] args) throws Exception{
        try(var fis = new FileInputStream("c:\\temp\\test.txt");
            var isr = new InputStreamReader(fis)){
            while(isr.ready()){
                isr.skip(1);
                int i = isr.read();
                char c = (char) i;
                System.out.print(c);
            }
        }
    }


Select 1 option(s):
39.

Question:

Identify correct statements about the following code:

import java.util.*;

class Student{
   int marks;
}  

class TestClass {
    var k = new Student(); //1

    public static void main(String[] args) {

       var s = new Student(){    //2
                @Override
                public String toString(){ return "student obj"; };
               };

       var slist = Set.of(new Student()); //3

       for(var i : slist){  //4
            System.out.println(i);
       }
    
       slist.removeIf((var s1) -> s1.marks<0); //5
    }
}


Select 1 option(s):
40.

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

HashSet<String> keys = new HashSet<>(List.of("a", "b", "c"));
ArrayList<String> values = new ArrayList<>(Set.of("1", "2", "3"));

Map<String, String> m = new HashMap<>();
int i = 0;
for(var key : keys){
    m.put(key, values.get(i++));
}
keys.clear();   //2
values.clear();
System.out.println(m.keySet().size()+" "+m.values().size());

Select 1 option(s):
41.

Question:

Given:

public static void createFile(String name) throws Exception{
    try (
            OutputStream os = new FileOutputStream(name);  ) {

        //INSERT CODE HERE       

        //flush and close the streams that are open
    }
    
}


Which of the following combinations of the lines of code and their outcome when inserted above, are correct?

Select 2 option(s):
42.

Question:

Given:

var qr = "insert into USERINFO values( ?, ?, ?)";
try(PreparedStatement ps =  c.prepareStatement(qr);)
{
    ps.setObject(1, 1, JDBCType.INTEGER);
    ps.setObject(2, "Ally A", JDBCType.VARCHAR);
    ps.setObject(3, "101 main str", JDBCType.VARCHAR);
    var i = ps.executeUpdate(); //1
    ps.setObject(1, 2, JDBCType.INTEGER);
    ps.setObject(2, "Bob B", JDBCType.VARCHAR);
    i = ps.executeUpdate(); //2
}


What will be the result?

Select 1 option(s):
43.

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

class Boo implements Serializable {
    transient int ti = 10;
    static int si = 20;
}

public class TestClass
{
    public static void main(String[] args) throws Exception
    {
     
        Boo boo = new Boo();
        boo.si++;
        System.out.println(boo.ti+" "+boo.si);
        var fos = new FileOutputStream("c:\\temp\\boo.ser");
        var os = new ObjectOutputStream(fos);
        os.writeObject(boo);
        os.close();
        
        var fis = new FileInputStream("c:\\temp\\boo.ser");
        var is = new ObjectInputStream(fis);
        boo = (Boo) is.readObject();
        is.close();            
        System.out.println(boo.ti+" "+boo.si);
    }
}

Select 1 option(s):

44.

Question:

Given the following code that appears inside a method:

   var values = new ArrayList<String>();
   //INSERT CODE HERE

What can be inserted at the given location without causing any compilation error?

Select 2 option(s):
45.

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 2 option(s):
46.

Question:

You have a file named customers.dat in c:\company\records directory. You want to copy all the lines in this file to another file named clients.dat in the same directory and you have the following code to do it:

    public static void writeData() {
        Path p1 = Paths.get("c:\\company\\records\\customers.dat");

        //LINE 20  - INSERT CODE HERE

        try (
                var br = new BufferedReader(new FileReader(p1.toFile()));
                var bw = new BufferedWriter(new FileWriter(p2.toFile()))) {
            String line = null;
            while ((line = br.readLine()) != null) {

                bw.write(line);
                bw.newLine();
            }
        }catch(Exception e){
            e.printStackTrace();
        }
    }


Which of the following options can be inserted independent of each other at //LINE 20 to make it work?
Assume that the current directory for the program when it runs is c:\code.

Select 2 option(s):
47.

Question:

Given:

public class TestClass {
   public  void myMethod(String... params)  {
       var a  = params;
       var b = params[0];
   }
}

What are the types of the variables a and b?

Select 1 option(s):
48.

Question:

Given:

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


Identify correct statements about the above module.

Select 1 option(s):
49.

Question:

Given:

package loops;
public class JustLooping {
    private int j;
    void showJ(){
        while(j<=5){
            for(int j=1; j <= 5;){
                System.out.print(j+" ");
                j++;
            }
            j++;
        }
    }
    public static void main(String[] args) {
        new JustLooping().showJ();
    }
}


What is the result?

Select 1 option(s):
50.

Question: Which statements about the following code contained in BankAccount.java are correct?

interface Account{
  public default String getId(){
     return "0000";
  }
}

interface PremiumAccount extends Account{
  public String getId();
}

public class BankAccount implements PremiumAccount{
  public static void main(String[] args) {
      Account acct = new BankAccount();
      System.out.println(acct.getId());
  }

}

Select 1 option(s):
51.

Question: Which of the following method definitions will prevent overriding of that method?

Select 4 option(s):
52.

Question:

Consider the following class...

class TestClass{
void probe(Object x) { System.out.println("In Object"); } //3
void probe(Number x) { System.out.println("In Number"); } //2
void probe(Integer x) { System.out.println("In Integer"); } //2
void probe(Long x) { System.out.println("In Long"); } //4
public static void main(String[] args){
double a = 10;
new TestClass().probe(a);
}
}

What will be printed?

Select 1 option(s):
53.

Question: Which of the following lines will cause the compilation to fail?

interface I { }

public enum EnumA implements I, Serializable { A, AA, AAA};  //1


class TestClass
{
    public enum EnumB{ B, BB, BBB;
     public Object clone(){ return B; } //2
   }

   public static enum EnumC{ C, CC, CCC };

   public static enum EnumD extends EnumC{ DDD }; //3

   public TestClass()
   {
      System.out.println(EnumC.CC.index()); //4
   }


   public static void main(String[] args)
   {
       System.out.println(EnumC.valueOf("ccc")); //5
       System.out.println(EnumC.CCC.name()); //6
   }
}

Select 3 option(s):
54.

Question:

Given:

module broker{
  exports org.broker.api;
}


The broker module contains org.broker.api.Installer interface, which is implemented by com.foo.AppInstaller class of com.foo module.

Which of the following is a valid module definition for  com.foo module?

Select 1 option(s):