Answer To: Q1:What does each of the following print? Explain each outcome. 1.System.out.println(2 + "bc");...
Kshitij answered on May 10 2021
Q1:What does each of the following print? Explain each outcome.
1. System.out.println(2 + "bc");
2. System.out.println(2 + "bc" + 3);
3. System.out.println( 2 + 3 + "bc");
4. System.out.println("bc" + (2 + 3) );
5. System.out.println("bc" + 2 + 3 );
Ans 1: 2bc
Ans2: 2bc3
Ans3:5bc
Ans4:bc5
Ans5:bc23
Q2:
Write out a program trace to find out what operation the following Java method performs, using the following example inputs: N = 17, N = 25.
public static Boolean mystery (int N) {
if (N<2) return false;
for (int i=2; i*i<=N; i++) {
if (N%i==0) {
return false;
}
}
return true;
}
Solution :--
1 for N= 17 when we call the method mystery firstly it chech n is les than 2 or not and answer is not than it go next start loop from 2 to N and i*I is less than N means loop for sqrt of N if n is divisible by I then it return false ;
Now for 17 no value which divides 17 then it return 17
2->> For 25 loop run for sqrt of 25 means 2 to 5 it is divisible by 5 so it return true;
Q3: Show, using a memory trace, how an insertion sort sorts the sequence of characters below:
E A G E R N E S S
Solution:-------
pass 0 [E, A, G, E, R, N, E, S, S]
pass 1 [A, E, G, E, R, N, E, S, S]
pass 2 [A, E, G, E, R, N, E, S, S]
pass 3 [A, E, E, G, R, N, E, S, S]
pass 4 [A, E, E, G, R, N, E, S, S]
pass 5 [A, E, E, G, N, R, E, S,...