Leave a Reply 3

Your email address will not be published. Required fields are marked *


renko

renko

Answer: D

Code question:
public class CharToStr {
public static void main(String[] args) {
String str1 = “Java”;
char str2[] = {‘J’, ‘a’, ‘v’, ‘a’};
String str3 = null;
for (char r : str2) {
str3 str3 + c;
}
if (str1.equals(str3))
System.out.println(“Succesful”);
else
System.out.println(“Unsuccesful”);
}

}
Output:
Exception in thread “main” java.lang.RuntimeException: Uncompilable source code – variable str3 is already defined in method main(java.lang.String[])
at CharToStr.main(CharToStr.java:7)

kishan sai gondi

kishan sai gondi

it is
for(char c : str2){
str3 = str3+c;
}

NOT:

for(char r:str2){
str3 str3+ c;
}
it is a typo error!we all go through it.

kishan sai gondi

kishan sai gondi

package com.sampletest.java;
public class CharToStr {
public static void main(String[] args) {
String str1 = “Java”;
char str2[] = {‘J’, ‘a’, ‘v’, ‘a’};
String str3 = null;
for (char c : str2) {
str3 = str3 + c;
}
if (str1.equals(str3))
System.out.println(“Succesful”);
else
System.out.println(“Unsuccesful”);
}

}

Output:
Unsuccesful

Answer: A