Given: What is result?
A.
Unsuccessful
B.
Compilation fails
C.
Successful
D.
An exception is thrown at runtime
Explanation:
Given: What is result?
Given: What is result?
A.
Unsuccessful
B.
Compilation fails
C.
Successful
D.
An exception is thrown at runtime
Explanation:
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)
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.
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