Quiz Space

Programming Concepts using Java · Quiz 1 · 25 Feb 2024 · January 2024 term

Question 12: Consider the code given below that checks whether two Pe…

Question 12

+7 marksOne correct option

Consider the code given below that checks whether two Persons are the same. Method equals is overridden to compare two Person objects as follows. If two Persons have the same name and age, then they are the same. Based on the given information, answer the question that follows.

java
class Person {
private String name;
private int age;
// Constructor to initialize instance variables
public String toString() {
return name + " (" + age + " years old)";
}
public boolean equals(Object obj) {
//CODE BLOCK
}
}
public class TestPerson {
public static void main(String[] args) {
Person person1 = new Person("Alice", 25);
Person person2 = new Person("Alice", 25);
if (person1.equals(person2)){
System.out.println(person1 + " and " + person2 + " are the same");
}
else{
System.out.println(person1 + " and " + person2 + " are different");
}
}
}

Choose the correct option to fill in place of CODE BLOCK so that the output is:
Alice (25 years old) and Alice (25 years old) are the same

  1. A
  2. B
  3. C
  4. D
Show answer

Correct answer

  • C

Question 12 of 15 in the IIT Madras BS Programming Concepts using Java (Java) Quiz 1 paper sat on 25 Feb 2024, in the January 2024 term (IIT M DIPLOMA AN2 EXAM QDD2 25 Feb 2024). It carries 7 marks.

More questions from this paper

  1. Q1Which of the following statements is/are true about activation records?
  2. Q2Figure question
  3. Q3Consider the code given below. Choose the correct option.
  4. Q4Consider the Java code given below. Choose the correct option regarding the given code.
  5. Q5Consider the Java code given below. Choose the correct option.
  6. Q6Consider the Java code given below. Choose the correct option.
  7. Q7Consider the code given below. Choose the correct option to fill in place of CODE BLOCK so that the output is: Processi…
  8. Q8Consider the Java code given below. What will the output be?
  9. Q9Consider the Java code given below. What will the output be?
  10. Q10Consider the Java code given below. Choose the correct option to fill in the CODE SEGMENT so that the output is:\ Toyot…
  11. Q11Consider the following code: Choose the correct option to fill in the CODE SEGMENT so that the output is:\ Animal makes…
  12. Q13Consider the Java code given below. Choose the correct option regarding the above code.
  13. Q14Consider the code given below. Choose the correct option.
  14. Q15Consider the Java code given below. Choose the correct option.