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 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?
What is printed as a result of executing the code segment?
/ CASE 1: / Iterator iterator = list.iterator();
while (iterator.hasNext()) {
Integer element = iterator.next();
if(element%2 == 0) iterator.remove();
/ CASE 2: /
/ CASE 3: /
}
public class Question02 {
}
public class Question03 {
public class Question04 {
}
class Forecast{ public int temperature; public int pressure; }
class Parent{
}
class Child extends Parent{
}
public class Question05 { public static void main(String[] args) { Parent parent = new Child(); parent.publicMethod();
}
public class Question06 {
}
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 {
}
class Car extends Vehicle {
}
public class Question08 {
}
class Todo { String day;
}
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"); } }
public class Question10 {
}
class ParentA { public ParentA() { System.out.println("Contructor Parent"); }
}
class ChildA extends ParentA {
}