package studentBase;
class Test
{
String greet = “Hi”;
String name = “Smedley”;
String nickName = name.substring (0,2); If (nickName == name.substring(0,2))
System.out.println(“has real nickname”);
else if (greet + name == greet + nickName);
System.out.println(“no real nickname”);
Else
System.out.println(“hmmm ... changed names?”); }
Will it compile? YES or NO. Give reason, if No.
NO, the program will not compile.
This is because, first there is no main method which is defined and in java the execution of any program starts in the main method.
There are still syntax errors, for example the if key word starts with a capital letter and it should be a small letter i i.e. if instead of If. Also the else keyword starts with a capital letter E instead of e i.e. Else instead of else.
THE FOLLOWING I THE CODE THAT WILL COMPILE
package studentBase;
public class Test {
public static void main(String[] args) {
String greet = "Hi";
String name = "Smedley";
String nickName = name.substring(0, 2);
if (nickName == name.substring(0, 2))
{
System.out.println("has real nickname");
}
else if (greet + name == greet + nickName)
{
System.out.println("no real nickname");
}
else {
System.out.println("hmmm ... changed names?");
}
}
}
Comments
Leave a comment