1. What is wrong with the following if statement (there are at least 3 errors). The indentation indicates the desired behavior. if (numNeighbors >= 3 ) numNeighbors++; System.out.println("You are no longer living!") else () { --numNeighbors; } 2. Describe the output produced by this poorly indented program segment: int number = 4; double alpha = -1.0; if (number > 0) if (alpha > 0) System.out.println("Here I am!"); else System.out.println("No, I'm here!"); System.out.println("No, actually, I'm here!"); 3. Consider the following if statement, where doesSignificantWork, makesBreakthrough, and nobelPrizeCandidate are all boolean variables: if (doesSignificantWork && makesBreakthrough) { nobelPrizeCandidate = true; } else { nobelPrizeCandidate = false; } Write a single assignment statement that does the same thing as this decision without an if/else.
Added by Heather V.
Close
Step 1
The first error is that the else statement is missing a statement or expression to execute. It should have a valid code block or statement after the else keyword. Show more…
Show all steps
Your feedback will help us improve your experience
Paul Gabriel and 91 other AP CS educators are ready to help you.
Ask a new question
Labs
Want to see this concept in action?
Explore this concept interactively to see how it behaves as you change inputs.
Key Concepts
Recommended Videos
Find mistakes in the below program and rewrite the correct program: A = 5 B = 4 if A == B: print("Hello") else: print("Hi")
Vandit T.
Retype the below code. Fix the indentation as necessary to make the program work. if 'New York' in temperatures: if temperatures['New York'] > 90: print('The city is melting!') else: print('The temperature in New York is', temperatures['New York']) else: print('The temperature in New York is unknown.') Sample output with input: 105 The city is melting!
Oswaldo J.
Consider the following if statement, which is syntactically correct, but uses poor style and indentation: if $(x>y)$ if $(y>0) x=x * y ;$ else if $(y<4) x=x-y$ Assume that $\mathrm{x}$ and $\mathrm{y}$ are int variables containing the values 9 and $3,$ respectively, before execution of the preceding statement. After execution of the statement, what value does $x$ contain? a. 9 b. 1 c. 6 d. 27 e. none of the above
Recommended Textbooks
Computer Science and Information Technology
Introduction to Programming Using Python
Computer Science - An Overview
18,000,000+
Students on Numerade
Trusted by students at 8,000+ universities
Watch the video solution with this free unlock.
EMAIL
PASSWORD