What will be the output of the following Java program? package pkg; class output {public static void main(String args[]) { StringBuffer s1 = new StringBuffer("Hello"); s1.setCharAt(1, x); System.out.println(s1); } }
Question
What will be the output of the following Java program? package pkg; class output {public static void main(String args[]) { StringBuffer s1 = new StringBuffer("Hello"); s1.setCharAt(1, x); System.out.println(s1); } }
Solution
The output of the Java program will be an error. The reason is that the character 'x' in the line s1.setCharAt(1, x); is not enclosed in single quotes. In Java, characters should be enclosed in single quotes.
If you want to replace the second character of the string "Hello" with 'x', you should write the line as s1.setCharAt(1, 'x');.
After this correction, the output of the program will be "Hxlo".
Similar Questions
What will be the output of the following Java program? class mainclass { public static void main(String args[]) { char a = 'Z'; a++; System.out.print((int)a); } }Options9091122123
What will be the output of the following program?class string_class {public static void main(String args[]){String obj = "hello";String obj1 = "world"; String obj2 = obj;obj2 = " world";System.out.println(obj + " " + obj2); }}
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); }}
What will be the output of the below program?public class Test { public static void main(String[] args) { char c = 65; System.out.println("c = " + c); }} Note : explain clearly in the answer
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
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.