Way to declare & initialize a String variable named "message" with a value of "Hello World" in Java?

Published: 24 April 2023
on channel: SDET Automation Testing Interview Pro
812
20

JAVA : SELENIUM : Correct way to declare and initialize a String variable named "message" with a value of "Hello World" in Java?

SDET Automation Testing Interview Questions & Answers

We will be covering a wide range of topics including QA manual testing, automation testing, Selenium, Java, Jenkins, Cucumber, Maven, and various testing frameworks.

JAVA : SELENIUM : Correct way to declare and initialize a String variable named "message" with a value of "Hello World" in Java?

a) String message = "Hello World";
b) message = "Hello World";
c) string message = new string("Hello World");
d) String message = new String("Hello World");

Answer: (a) String message = "Hello World";

Explanation:

Option (b) is incorrect because it is missing the type declaration.

Option (c) is incorrect because it is using the wrong class name and syntax to create a new instance of the String class.

Option (d) is incorrect because it is using the constructor of the String class to create a new instance, which is unnecessary since string literals are automatically converted to String objects.

Option (a) is the correct way to declare and initialize a String variable in Java, using the type declaration "String", the variable name "message", the assignment operator "=", and the value "Hello World" enclosed in double quotes.


Watch video Way to declare & initialize a String variable named "message" with a value of "Hello World" in Java? online without registration, duration hours minute second in high quality. This video was added by user SDET Automation Testing Interview Pro 24 April 2023, don't forget to share it with your friends and acquaintances, it has been viewed on our site 812 once and liked it 20 people.