Java Quiz

Java questions:

1. Java question related to == operator and equals() method.

String a1 = “xyz“;
String a2 = “xyz“;

String a3= new String(“xyz“);

String s1= “Selenium“;

String s2 = “Selenium“;

String s3 = “Selenese“;

System.out.println(“Is a1 == a2 ? “+(a1==a2)); //returns true
System.out.println(“Is a1 == a3 ? “+(a1==a3)); //returns false

System.out.println(s1.equals(s2)); // true  

System.out.println(s1.equals(s3)); // false

System.out.println(“Is a1 equals a3 ? “+(a1.equals(a3))); //returns true

The == operator is used to check the object of the two strings. If the object is same, then it returns true else returns false.

The equals() method is used to check the value of the strings. Strings are immutable in Java. So whenever you try to change the value of a string, a new instance will come to the string.

2. Is string thread-safe in java?

Ans: Yes. Strings are immutable so we can not change its value in program. Hence string is thread-safe and we can use strings in multi-threaded environment.

3. Is static public void correct?
Ans: Yes. Program complies without any issues.

4. Can we inherit the constructor?

Ans: No. Constructor can not be inherited.

5. Can we override final method?

Ans: Final methods can not be overridden.

6. Which one is the correct way to declare and initialize an array?
A. int [] arr = {’34’, ’98’, ‘3’};
B. int [] arr = (6, 7,9, 3);
C. int arr [] = {12, 11, 13};
D. int arr [] [] = {1, 2, 3, 4};

Ans: C

7. Which of the following is correct?

a. String str[];

b. String str[9];

c. Both are correct.

Ans: A.

Reason: Because we can not declare an array with size.

8. Which one is the valid declaration for a boolean?

a . boolean bn = False;

b. boolean bn = “true”;

c. boolean bn = false;

d. boolean bn = wrong;

Ans: C.

Reason: Boolean accepts only true or false.

9. What is the output of below code?

Boolean b1 = new Boolean (true);
System.out.println(“Value of b1 is: “+b1);

a. Compilation error.

b. Value of b1 is: null

c. Value of b1 is: true

Ans: C

10. What is the output of below code?

Boolean b2 = new Boolean (“true”);
System.out.println(“Value of b2 is: “+b2);
Boolean b3 = new Boolean (“”);
System.out.println(“Value of b3 is: “+b3);

Ans: Value of b2 is: true
Value of b3 is: false

11. Find the output of below Substring.

public void subString(){
String str = “Selenium99 Tutorials”; //
System.out.println(“String length is: “ + str.length());
str.substring(0, 10);
System.out.println(“Substring value is: “ + str);
}

Ans:

String length is: 20
Substring value is: Selenium99 Tutorials

12. Find the output of below Substring.

public void subString2(){
String str = “Selenium99 Tutorials”; //
System.out.println(“String length is: “ + str.length());
String str2 = str.substring(0, 10);
System.out.println(“Substring value is: “ + str2);
}

Ans:

String length is: 20
Substring value is: Selenium99

13.What is the output of below code snippet?

public void subString2(){
String str = “Selenium99”; //
System.out.println(“String length is: “ + str.length());
String str2 = str.substring(0, 11);
System.out.println(“Substring value is: “ + str2);
}

Ans:

String length is: 10
Exception in thread “main” java.lang.StringIndexOutOfBoundsException: String index out of range: 11
at java.lang.String.substring(Unknown Source)