Jacobvu84 / selenium-vietnam-training-course

Questions Tracking
7 stars 5 forks source link

Core Java 02: Total 18 Questions #30

Open Jacobvu84 opened 6 years ago

Jacobvu84 commented 6 years ago
  1. Consider the following code segment and class
    
    Widget w = new Widget(66);

System.out.println(w.getWidgets());

Class Widget{ private int numWidgets;

public Widget(int numWidgets){
    numWidgets=numWidgets;
}

public int getWidgets(){
    return numWidgets;
}

}

What is printed as a result of executing the code segment?

A. 66
B. a memory reference value
C. 0
D. a comlile error mesage
E. an Exception error message

6. Consider the following code segment and class

Employee jacob = new Employee();

System.out.println(jacob.age());

Class Employee { int age;

public Employee(){
    age=30;
}

}


What is wrong with this program?

A. It does not compile, because age cannot accessed directly.
B. The intent of encapsulation is violated, because age can be accessed directly.
C. The constructor has no parameter.
D. There is no value assigned to the age instance variable
E. There is nothing wrong with the program.

7. Consider the following code segment and two class

Mambo m = new Mambo(); Rumba m = new Rumba();

class Rumba{ public Rumba(){ System.out.println(" Executing the Rumba constructor"); } }

class Mambo{ public Mambo(){ System.out.println(" Executing the Mambo constructor"); } }


What is the relationship between class Rumba and class Mambo ?

A. Inheritance only
B. Composition only
C. Both inheritance and composition
D. Polymorphism
E. There is no class relationship between Rumba and Mambo

8. Consider the following code segment and class

Bank jacob = new Bank(500.0); Bank bella = jacob;

bella.makeDeposit(225.0); jacob.makeWithdrawal(100.0);

System.out.println(bella.getBalance() + " " + jacob.getBalance());

class Bank{

private double balance; public Bank(double amount){ balance = amount; }

public double getBalance(){
    return balance;
}

public void makeDeposit(double amount){
    balance+=amount;
}

public void makeWithdrawal(double amount){
    balance-=amount;
}

}

What is printed as a result of executing the code segment?

A. 625.0 625.0
B. 400.0 752.0
C. 725.0 400.0
D. 400.0 400.0
E. 725.0 725.0

9. Consider the following method.

/*Precondition: list is a non-empty array. /

public static void SynergixTest(int[] list){ for(int p = 0; p < list.length ; p++){ int max = list.length - 1; int temp = list[p]; list[p] = list[max-p]; list[max-p] = temp; } }

Which of the following correctly describes the result of calling SynergixTest ?

A. The element in the list array are in random order.
B. The list array is sorted in descending order
C. The list array is sorted in ascending order.
D. The element in the list array are in reverse order.
E. The element in the list array are appear unchanged.

10. Consider the following code segment.

int list = {5,10,15,20,25,20,15,10,5}; int max = list.length-1; for(int k = max; k > 0 ; k--) list[k] = list[k]/list[max];

for(int k = 0; k < list.length ; k++)
    System.out.println(list[k] + " ");
What will be printed as a result of executing the code segment?

A. 5 10 15 20 25 1 10 15 20
B. 1 2 3 4 5 4 3 2 1
C. 5 10 15 20 25 20 15 10 1
D. 1 1 1 1 1 1 1 1 1
E. 5 5 5 5 5 5 5 5 5

11. Consider the following code segment.

int[][] matrix = {{11,22,33},{44,55,66},{77,88,99}};

for(int row = 1; row < 3 ; row++)
    for(int col = 0; col < 3 ; col++)
        matrix[row][col] = matrix[row-1][col]

System.out.println(matrix[row][col] + " ");

Which of the following describes which values, stored in matrix, are left unchanged after the code segment executes?

A. All the values in the matrix are the same
B. All the values in the matrix are changed
C. Every row in the matrix is the same
D. Every col in the matrix is the same
E. Both diagonals in the matrix are the same

12. Consider the following code segment.

int[][] matrix = new int[3][4]; int[][] matrix = {{1,1,1},{2,2,2},{3,3,3}}; for(int row : matrix){ for(int number : row){ System.out.print(number + " "); System.out.println(); } }

What is printed as a result of executing the code segment?

A. A compile error message

B. An ArrayIndexOutOfBoundsException error message

C. 1 1 1 2 2 2 3 3 3

D. 1,1,1,2,2,2,3,3,3

E. 
 1  1  1  1
 2  2  2  2
 3  3  3  3

13. Consider the following code segment.

List list1 = new ArrayList(); List list2 = new ArrayList();

for(int k = 10; k < 20 ; k++) list1.add(k); for(int number:list1) list2.add(0, number);

System.out.print(list2);

What is printed as a result of executing the code segment?

A. [10, 11, 12, 13, 14, 15, 16, 17, 18, 19]
B. [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
C. [19, 18, 17, 16, 15, 14, 13, 12, 11, 10]
D. [0, 10, 0, 11, 0, 12, 0, 13, 0, 14, 0, 15, 0, 16, 0, 17, 0, 18, 0, 19]
E. An ArrayIndexOutOfBoundsException message

14. Consider the following code segment.

int[] numbers = {56, 34, 76, 89, 32, 18, 37, 75, 99, 55}; List temp = new ArrayList();

for(int number:numbers) temp.add(number); reduce(temp); System.out.println(temp);

public static List reduce(List temp){ for(int k = 0; k < temp.size(); k++) if(k%2==0) temp.remove(k); return temp; }

What is printed as a result of executing the code segment?

A. [34, 76, 32, 18, 75, 99]
B. [34, 89, 18, 75, 55]
C. [56, 76, 32, 37, 99]
D. [89, 37, 75, 99, 55]
E. An ArrayIndexOutOfBoundsException message

15. Consider the following code segment and three class

Hybrid prius = new Hybrid();

class Engine{ public Engine(){ System.out.print("Executing the Engine constructor"); } }

class Car{ private Engine engine;

public Car(){
    System.out.print("Executing the Car constructor");
}

}

class Hybrid extends Car{

public Hybrid(){
    System.out.print("Executing the Hybrid constructor");
}

}

What is printed as a result of executing the code segment?

A. Executing the Hybrid constructor

B. Executing the Car constructor
    Executing the Hybrid constructor

C. Executing the Car constructor
    Executing the Engine constructor
    Executing the Hybrid constructor

D. Executing the Engine constructor
    Executing the Hybrid constructor

E. An Exception error message

16. Consider the following code segment

class Person{ protected int age; public Person(int a){ age = a; } }

class Student extends Person{ protected double gpa; public Student (int a, int g){ super(a); gpa= g; } }

class Course {

private Student[] students;

public Course(int size, int a, double g){
    Students = new Student[size];
    for(int k = 0; k < students.lenght; k++)
        students[k] = new Student(a,g);
}

public void displayCourse(){
    /* Missing code */
}

}

Consider the following code segment and the previously mentioned three classes.

int count = 6; // number of students in the course int age = 15; // initial age for each student int gpa = 3.14; // initial gpa for each student

Course selenium = new Course(count, age, gpa); selenium.displayCourse();

Which of the following implementation of /* Missing code*/ will display the attribute values of each Student object

Implementation 01

for(int k = 0; k < students.lenght; k++) System.out.print(students[k]);

Implementation 02

for(Students student: students) System.out.print(student);

Implementation 03

System.out.print(students);


17. Select the interface or the class to start designing the program.

interface Circus{ public void costume(); public void entrance(); public void announcement(); public void act(); public void performance(); public void exit(); }

class Circus{ public void costume() { / code /} public void entrance() { / code /} public void announcement() { / code /} public void act() { / code /} public void performance() { / code /} public void exit() { / code /}

}

Which of the following is the most plausible argument for your selection?

A. The class approach is better, because it has immediate functional methods that can be tested and improved. It allows the programmers to get to work quicker.
B. The class approach is better, because polymorphism is part of the future program design as new acts are added to the show. Polymorphism is much easier with an interface umbrella.
C. The class approach is better, because polymorphism is part of the future program design as new acts are added to the show. Polymorphism is much easier with an class umbrella.
D. The class approach is better, because it allows maximum flexibility between the programming team and the client about the requirements without any concerns yet about implementations.
E. It simply does not matter. It is strickly a personal preference and both the class approach and interface approach work equally well for program design.

18. Select the immediate concrete class implementations or the abstract class intermediate approach.

interface Circus{ public void costume(); public void entrance(); public void announcement(); public void act(); public void performance(); public void exit(); }


Which of the following is the most plausible argument for your selection?

A. The intermediate abstract class approach is better, because it allows implementation of all the common methods that have identical actions first, and then the individual performing class focus on the differences. It will also allow the addition of a new circus performer with minimal additional code.
B. The intermediate abstract class approach is better, because polymorphism can already start at the abstract class level and does not need to wait for concrete class extensions.
C. The immediate concrete class implementation is better, because it can specifically construct individual objects and get down to the business of testing a realistic program.
D. The immediate concrete class implementation is better, because polymorphism is only possible with the implementation of methods at the concrete class level.
E. A proper decision cannot be made until it is know how the program will change in the future.