Find the output of the below Java program?public class Test { public static void main(String[] args) { Test t1 = new Test(); Test t2 = t1; System.out.println(t1.equals(t2)); }}A) trueB) falseC) nullD) error
Question
Find the output of the below Java program?public class Test { public static void main(String[] args) { Test t1 = new Test(); Test t2 = t1; System.out.println(t1.equals(t2)); }}A) trueB) falseC) nullD) error
Solution
The output of the given Java program will be:
A) true
Explanation:
In the given Java program, we are creating an object 't1' of class 'Test'. Then, we are assigning the reference of 't1' to 't2'. So, both 't1' and 't2' are pointing to the same object.
The 'equals()' method in Java is used to compare two objects for equality. In this case, since 't1' and 't2' are referring to the same object, 't1.equals(t2)' will return true. Hence, the output of the program will be 'true'.
Similar Questions
What will be the output of the following Java program?class mainclass { public static void main(String args[]) { boolean ct1 = true; boolean ct2 = false; if (ct1) System.out.println(ct1); else System.out.println(ct2); } }Options10truefalse
What will be the output of the program?class Test { public static void main(String [] args) { Test p = new Test(); p.start(); } void start() { boolean b1 = false; boolean b2 = fix(b1); System.out.println(b1 + " " + b2); } boolean fix(boolean b1) { b1 = true; return b1; }}true truefalse truetrue falsefalse false
ect answerWhat will be the output of the following Java program?class java { public static void main(String args[]) { int var1 = 3; int var2 = 8; if ((var2 = 2) == var1) System.out.print(var2); else System.out.print(++var2); } }
ind the output of the below Java program?class Student { int id; Student(int id) { this.id = id; }}public class Test { public static void main(String[] args) { Student s1 = new Student(1001); System.out.println(s1.equals(null)); }}A) trueB) falseC) errorD) Exception
e correct answerWhat is the output of the following Java program?class Main { public static void main(String args[]) { final int i; i = 20; i = 30; System.out.println(i); }}
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.