Question

In: Computer Science

JAVA What is the output? Explain how you obtain this answer by hand, not using a...

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); }

Solutions

Expert 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.


Related Solutions

• Using the Scanner class to obtain input from the user. • Using printf to output...
• Using the Scanner class to obtain input from the user. • Using printf to output information to the user. • Using arithmetic operators to perform calculations. • Using if statements to make decisions based on the truth or falsity of a condition. • Using relational operators to compare variable values. Project 1: Write an application that asks the user to enter two integers, obtains them from the user and prints their sum, product, difference and quotient (division). Sample Output:...
HOW WOULD YOU COMPUTE THIS BY HAND AND USING A TI-84 CALCULATOR? WHAT ARE THE STEPS...
HOW WOULD YOU COMPUTE THIS BY HAND AND USING A TI-84 CALCULATOR? WHAT ARE THE STEPS FOR THE CALCULATOR? also for 2, how do you calculate PMT for TVM solver on Calc Consider the after-tax cash flows below from a project that is being considered by Despondus Corporation. Since the project is an extension of the firm's current business, it carries the same risk as the overall firm. Year 0 1 2 3 4 5 Cash Flow -$264,000 $92,000 $66,000...
Using Java, Explain how to implement a functional interface using a lambda expression. You may include...
Using Java, Explain how to implement a functional interface using a lambda expression. You may include small sections of code to illustrate your explanation.
Show and explain how to obtain the echelon form of any matrix using Matlab.
Show and explain how to obtain the echelon form of any matrix using Matlab.
USING THE IF STATEMENT & Scanner for input and println for output - Write a java...
USING THE IF STATEMENT & Scanner for input and println for output - Write a java program where the user enters a temperature as a whole number from input, and outputs a “most likely” season [either SUMMER, SPRING, FALL or WINTER] depending on the temperature entered. SUMMER would be 90 or higher SPRING   would be 70 to less than 90 FALL       would be 50 to less than 70 WINTER would be less than 50 Consider it an error if the...
PLEASE explain how you arrived at each answer. Thank you. ……………………Inputs per unit of strawberry output………Inputs...
PLEASE explain how you arrived at each answer. Thank you. ……………………Inputs per unit of strawberry output………Inputs per unit of curtain output Birdy Island                                         60                                                                           80 Ducky Island                                       40                                                                           40 Assume the following: Strawberry and curtain are the only two products produced in these islands. There are constant ratios of input to output whatever the level of output of strawberry and curtain. Lastly, competition prevails in all markets. 1.            Which country has comparative advantage in which good?                 A.            Birdy has...
What is the output of the following code? Explain your answer in a paragraph or two....
What is the output of the following code? Explain your answer in a paragraph or two. class ExceptionThrown { static int divideByZero(int a, int b){ int i = a/b; return i; }    static int computeDivision(int a, int b) { int res =0; try{    res = divideByZero(a,b); } catch(NumberFormatException ex){ System.out.println("NumberFormatException is occured"); }    return res; }    public static void main(String args[]){ int a = 1;    int b = 0; try{ int i = computeDivision(a,b); }...
Each question should be about a paragraph! Thank you:) 30.explain what copyrights are, how to obtain...
Each question should be about a paragraph! Thank you:) 30.explain what copyrights are, how to obtain them, and how they differ from trademarks 31.explain how adverse possession may sometimes vest title in real property despite the nonconsent of the owner
Write a program that produces the following output using nested for loop in Java. ****** ////////////...
Write a program that produces the following output using nested for loop in Java. ****** //////////// ****** ***** //////////\\ ***** **** ////////\\\\ **** *** //////\\\\\\ *** ** ////\\\\\\\\ ** * //\\\\\\\\\\ * \\\\\\\\\\\\
(Note: The answer has to be typed, not hand written nor a picture.) Thank you. How...
(Note: The answer has to be typed, not hand written nor a picture.) Thank you. How do you think using a EWMA chart versus a Shewhart control chart would/would not work in your realm of work
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT