JAVA What is the output Explain how you obtain this answer b

JAVA

What is the output? Explain how you obtain this answer by hand, not using a computer. String alphabet = \"ABCDEFGHIJKLMNOPQRSTUVWXYZ\"; for(int i = 1; i <= 26; i *= 2) { System.out.print(alphabet.charAt(i - 1)); }

What is the output? Explain how you obtain this answer by hand, not using a computer. int n = 500; int count = 0; while (n > 1) { if (n % 2 == 0) { n /= 3; } else { n /= 2; } count++; } System.out.println(count);

A Java Swing application contains this paintComponent method for a panel. Sketch what is drawn by this method. public void paintComponent(Graphics g) { super.paintComponent(g); g.setColor(Color.black); g.fillRect(100, 100, 200, 100); g.fillRect(300, 200, 200, 100); g.fillRect(500, 100, 200, 100); }

Solution

Part 1) Since i is iterating in powers of 2. So, we will get alphabet[1-1], alphabet[2-1], alphabet[2*2-1],alphabet[2*2*2-1],alphabet[2*2*2*2-1], which is alphabet[0], alphabet[1], alphabet[3], alphabet[7], alphabet[15] as i is limited to the value 26 and since we are using System.out.print; therefore result will be displayed in one single line. So, output will be ABDHP.

Part 2) Initially count is 0. Since, 500%2 == 0, now n becomes 500/3 = 166 and count is incremented and becomes 1. Since n>1, loop execution will continue.

Now, since, 166%2 == 0, now n becomes 166/3 = 55 and count is incremented and becomes 2. Since n>1, loop execution will continue.

Since, 55%2 != 0, now n becomes 55/2 = 27 and count is incremented and becomes 3. Since n>1, loop execution will continue.

Since, 27%2 != 0, now n becomes 27/2 = 13 and count is incremented and becomes 4. Since n>1, loop execution will continue.

Since, 13%2 != 0, now n becomes 13/2 = 6 and count is incremented and becomes 5. Since n>1, loop execution will continue.

Since, 6%2 == 0, now n becomes 6/3 = 2 and count is incremented and becomes 6. Since n>1, loop execution will continue.

Since, 2%2 == 0, now n becomes 2/3 = 0 and count is incremented and becomes 7. Since, n is not greater than 1 therefore while loop terminates here.

So, output will be 7.

Part 3) You need to paste the code and see the output yourself.

Hope it helps, do give your response.

JAVA What is the output? Explain how you obtain this answer by hand, not using a computer. String alphabet = \

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site