Jacobvu84 / selenium-vietnam-training-course

Questions Tracking
7 stars 5 forks source link

Core Java 03: Total 10 Questions #31

Open Jacobvu84 opened 6 years ago

Jacobvu84 commented 6 years ago
  1. What is printed as a result of executing the code segment?

    
    public class Question01 {
    
    public static void main(String[] args) {
    
        List<Integer> list = new ArrayList<Integer>();
        list.add(1);
        list.add(2);
        list.add(3);
        list.add(4);
        list.add(5);

/ CASE 1: / Iterator iterator = list.iterator(); while (iterator.hasNext()) { Integer element = iterator.next();
if(element%2 == 0) iterator.remove();

    }

/ CASE 2: /

      for (Integer element : list) {
        if(element%2 == 0){
            list.remove(element);
        }
    }

/ CASE 3: /

    List<Integer> listOdd = new ArrayList<Integer>();
    for (Integer element : list) {
        if(element%2 == 0){
            listOdd.add(element);
        }
    }
    list.removeAll(listOdd);

    for (int i = 0; i < list.size(); i++) {
        System.out.println(list.get(i));
    }
}

}


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

public class Question02 {

public static void main(String[] args) {

    // Case 1
    System.out.println(10 == 10);

    // Case 2
    System.out.println(10 == new Integer(10));

    // Case 3
    System.out.println(true | false & false);

    // Case 4
    System.out.println(new Integer(10) == new Integer(10));

    // Case 5
    int i = 0;
    System.out.println(1 == i++);

    int number = (int) Math.random();
    System.out.println("Number: " + number);
}

}

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

public class Question03 {

int questions = 100;

public static void main(String[] args) {
    Question03 mrA = new Question03();

    mrA.questions = 200;
    Question03 mrB = doubleTreeLeaves(mrA);

    System.out.println(mrA==mrB);

    System.out.println(mrA.questions);
    System.out.println(mrA.questions);
}

private static Question03 doubleTreeLeaves(Question03 mr) {
    mr = new Question03();
    mr.questions = 2 * mr.questions;
    return mr;

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

public class Question04 {

public static  void changeTheString(String weather){
    weather = "sunny";
}

public static  void changeArray(String[] rainDays){
    rainDays[1] = "Sunday";
}

public static  void changeTheObject(Forecast forecast){
    forecast.temperature = 35;
}

public static void main(String[] args) {
    String weather = "rainy";
    changeTheString(weather); // Value
    System.out.println("The weather is: " + weather);

    String[] rainDays = new String[]{"Monday", "Friday"};
    changeArray(rainDays); // Array
    System.out.println("The rain days were on: " +  rainDays[0] + " and " + rainDays[1]);

    Forecast forecast = new Forecast();
    forecast.pressure = 700;
    forecast.temperature = 20; // Object
    changeTheObject(forecast);
    System.out.println("The temperature is " + forecast.temperature);
}

}

class Forecast{ public int temperature; public int pressure; }

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

class Parent{

public Parent() {
    System.out.println("constructor Parent");
}

private void privateMethod(){
    System.out.println("parent.privateMethod");
}

protected void protectMethod() {
    System.out.println("parent.protectMethod");
    privateMethod();
}

public void publicMethod() {
    System.out.println("parent.publicMethod");
    protectMethod();
}

}

class Child extends Parent{

public Child() {
    System.out.println("constructor Child");
}

private void privateMethod(){
    System.out.println("Child.privateMethod");
}

@Override
protected void protectMethod() {
    System.out.println("Child.protectMethod");
    privateMethod();
}

@Override
public void publicMethod() {
    System.out.println("Child.publicMethod");
    protectMethod();
}

}

public class Question05 { public static void main(String[] args) { Parent parent = new Child(); parent.publicMethod();

}

}

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

public class Question06 {

public static void main(String[] args) {

    Set<Integer> set = new TreeSet<>();
    set.add(3);
    set.add((int) 3.0);
    set.add(2);
    set.add(2);
    set.add(Integer.parseInt("3"));

    System.out.println(set);
}

}


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

public class Question07{ public static void main(String[] args) { Car car = new Car(200,"Manual"); System.out.println(String.format("Max speed is %d, Coutry is %s", car.maxSpeed,Vehicle.country)); } }

class Vehicle {

public int maxSpeed;

public static String country;

public Vehicle(int maxSpeed) {
    this.maxSpeed = maxSpeed;
    country = "USA";
}

}

class Car extends Vehicle {

public String transmission;

public Car(int maxSpeed, String transmission) {
    super(maxSpeed);
    this.maxSpeed = 100;
    this.transmission = transmission;
}

static{
    country = "VIETNAM";
}

}

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

public class Question08 {

public static void main(String[] args) {
    Map<Todo, String> todos = new HashMap<>();

    Todo t;
    todos.put(t = new Todo("Monday"), "Task1");
    todos.put(new Todo("Monday"), "Task2");
    todos.put(new Todo("Tuesday"), "Task3");

    System.out.println("Size: " + todos.size() + ", t: " + todos.get(t));
}

}

class Todo { String day;

public Todo(String day) {
    this.day = day;
}

public boolean equals(Object o){
    return((Todo) o).day == this.day;
}

public int hashCode(){
    return 0;
}

}


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

public static void main(String[] args) { try { throw new FileNotFoundException(); } catch (FileNotFoundException e) { System.out.println(e.getClass().getSimpleName() + " - " + e.getMessage()); } catch (IOException e) { System.out.println(e.getClass().getSimpleName() + " - " + e.getMessage()); }catch (Exception e) { System.out.println(e.getClass().getSimpleName() + " - " + e.getMessage()); }finally { System.out.println("Finally"); } }

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

public class Question10 {

public static void main(String[] args) {
    ChildA a = new ChildA();
}

}

class ParentA { public ParentA() { System.out.println("Contructor Parent"); }

{
    System.out.println("       block code in Parent");
}

static {
    System.out.println("Static block code in Parent");
}

}

class ChildA extends ParentA {

public ChildA() {
    System.out.println("Contructor Child");
}

{
    System.out.println("       block code in Child");
}

static {
    System.out.println("Static block code in Child");
}

}