Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Will give thumbs up for correct answers to the questions. Also really appreciate

ID: 3902007 • Letter: W

Question

Will give thumbs up for correct answers to the questions. Also really appreciate if brief explanation is also provided as well,

Thank you
(All questions are about Java)


3. (3 x 2 marks) The following code snippets will not compile because of syntax errors. Find and correct the error(s) (you do not need to rewrite all of the code, just make the corrections clear). There is only one error per question and it is in the code that is shown to you Systen . out.print ("Guess my name : Strings -ySeanner.nextline while (s !. "Aaron") a) "It System.out-peintin("Wrong guess.Try agains -ayScanner mext Line 0 b)System.out-print ("Guess the secret munbers -3337 ystem.out-printin("Yay! ystem.out.printin(Boo." else )String) colours-redeyellow."green- String favouriteColour - colours 4. (3 x2 marks) What is the output of the following code snippets? there today!"; "It's String t . s-charAt (0) + · t+s.substzing (10.13) t-t + s.cha rAt (s.length ( ) a) String very windy . out 1); “ System out printia(e.tooppezCase)) b) public statie void main (String13 args) System.out printin(x) soneethod (x) Systen out println(x) pablic atatie void sonelethod(int x) 10 c) publie statie vold ain (String1 args) int1,2,3 System.out println(x[0) someMethod (x) a System.out println(x[01) publie statie vold soneethod(int1x for (int 0a icx.lengtha i

Explanation / Answer

3- Correct code:

a)

System.out.print("Guess any name: ");

String s=myScanner.nextLine();

while(!s.equals("Aaron")){

System.out.println("Wrong guess. Try again");

s=myScanner.nextLine();

}

we cannot directly compare a string to another without equals method.

b-

System.out.print("Guess the secret number ");

int x=myScanner.nextInt();

if(x==1337)

System.out.println("Tay!");

else

System.out.println("Boo");

for comparison we have to use == operator rather than =.

c- String[] colours={"red","blue","yellow","green"};
String favouriteColour=colours[0];

assignment from the array is done by using the index.

Q4-

a)String s="it's a very windy out there today !";
String t=s.charAt(0)+" ";
t=t+s.substring(10,13);
t=t+s.substring(s.length()-1);
System.out.println(t.toUpperCase());
the output will be I Y W! because first t=s.charAt(0) which is first character of the string which is i
then i is concatenated with s.substring(10,13) which means index 10 to 12 because substring takes one value less then the second argument so at 10 we have at 10 y then at 11 we have a space then at 12 we have w .
the again t is cocatenated with s.substring(s.length()-1) which is the last character ! and then t is converted to uppercase so that is why the output is I Y W!

b)int x=5;
System.out.println(x);
someMethod(x);
System.out.println(x);}
public static void someMethod(int x){
x=x*10;
}
the output will be 5
5 because when we pass x to someFunction then it multiplies x with 10 but does not return anything and also x is not a static variable to there is no change in the value of x in main function.

C)int x[]={1,2,3};
System.out.println(x[0]);
someMethod(x);
System.out.println(x[0]);}
public static void someMethod(int x[]){
for(int i=0;i<x.length;i++)
x[i]=x[i]*10;
}
the output will be 1
10 because when we pass x to someFunction then it multiplies every value of x array with 10 and it is changed because it directly changes in the array so the values in the array gets updated.

if you like the answer please provide a thumbs up.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote