Q1: Which of the following is a double-selection control statement?
|
|
|
|
Q2: Which of the following is not a Java keyword?
|
|
|
|
Q3: What is output by the following Java code segment?
int temp;
temp = 200;
if ( temp > 90 )
System.out.println( "This porridge is too hot." );
if ( temp < 70 )
System.out.println( "This porridge is too cold." );
if ( temp == 80 )
System.out.println( "This porridge is just right!" );
Conditional Operator (?:)
Q4: Which of the following is not true about the conditional operator ( ?: )?
Q5: What is output by the following Java code segment?
int temp = 50;
if ( temp > 90 )
{
System.out.println( "This porridge is too hot." );
// cool down
temp = temp – ( temp > 150 ? 100 : 20 );
} // end if
else
{
if ( temp < 70 )
{
System.out.println("This porridge is too cold.");
// warm up
temp = temp + (temp < 50 ? 30 : 20);
} // end if
} // end else
if ( temp == 80 )
System.out.println( "This porridge is just right!" );
Q6: A dangling-else can be clarified by using:
|
|
|
|
Q7: The empty statement is denoted with what symbol?
|
|
|
|
Q8: Which statement is false?
Q9: What is output by the following Java code segment?
int temp;
temp = 180;
while ( temp != 80 )
{
if ( temp > 90 )
{
System.out.print( "This porridge is too hot! " );
// cool down
temp = temp – ( temp > 150 ? 100 : 20 );
} // end if
else
{
if ( temp < 70 )
{
System.out.print(
"This porridge is too cold! ");
// warm up
temp = temp + (temp < 50 ? 30 : 20);
} // end if
} // end else
} // end while
if ( temp == 80 )
System.out.println( "This porridge is just right!" );
Q1: -> c. if…else
Eplanation: When the condition is true, it execute some statement and when the condition is false, it execute some other statement.
Q2: -> b. next
Q3: -> a. This porridge is too hot.
Explanation: When the temperature is 200, then only temp > 90 is true and then the statement written under this condition is executed.
Q4: -> c. The second operand is the result value if the condition evaluates to false.
Explanation: The syntax for "?:" conditional operator is given below.
(condition) ? return this if 'condition' is true : return this otherwise
Q5: -> d. None of the above.
Explanation: temp = 50 initially. temp>90 is false, so statement under this condition will not be executed. Then the control will go into else condition. Under else condition, temp<70 is true. So it will print "This porridge is too cold!" and excute the statement "temp = temp + (temp < 50 ? 30 : 20)" which will set the temp to 70. So temp==80 is also false and statements under it will not get excuted. So only "This porridge is too cold!" will be printed.
Q6: -> c. Braces {}
Q7: -> a. Semicolon ;
Explanation: Semicolon actually denotes the end of any statements. If nothing is written before it, then it becomes empty statement.
Q8: -> This question should be "Which statement is true?".
Ans - c. Syntax errors are caught by the compiler. Logic errors have effects at execution time. This is the only right statement and all others are wrong.
Eplanation: Syntax error happens when you have mistaken in writing the program. It means when you not followed the proper syntax. So at compile time compiler checks for that and will give you the syntax errors made by you. On the other hand logic errors have effect when the program is running(means execution time). Simple example is that you have written a statement to divide and the denominator becomes zero.
Q9: -> b. This porridge is too hot! This porridge is just right!
Explanation: temp = 180 initially. While loop will loop until temp = 80.In the while loop temp > 90 is true so "This porridge is too hot!" will be printed and "temp = temp – ( temp > 150 ? 100 : 20 );" will be executed which will set temp to 80. Now since the condition in while loop become false, so control will come out of the loop. Now temp == 80 is true, So "This porridge is just right!" will get printed.
Please give feedback.
Q1: Which of the following is a double-selection control statement? do…while for if…else if. Q2: Which...
Prelab Exercises Your task is to write a Java program that will print out the following message (including the row of equal marks): Computer Science, Yes!!!! ========================= An outline of the program is below. Complete it as follows: a. In the documentation at the top, fill in the name of the file the program would be saved in and a brief description of what the program does. b. Add the code for the main method to do the printing. //...
QUESTION 1 Which statement results in the value false? The value of count is 0; limit is 10. (count != 0)&&(limit < 20) (count == 0)&&(limit < 20) (count != 0)||(limit < 20) (count == 0)&&(limit < 20) 10 points QUESTION 2 If this code fragment were executed in an otherwise correct and complete program, what would the output be? int a = 3, b = 2, c = 5 if (a > b) a = 4; if (...
Please create a class in Java that completes the following conditions MorseTree.java /* This program will read in letters followed by their morse code * and insert them properly in a tree based on the amount of symbols * it has and whether they are left or right descendent. Finally * it prints a few certain nodes. */ import java.io.File; import java.io.FileNotFoundException; import java.util.Scanner; public class MorseTree { public static void main(String[] args) throws FileNotFoundException { //...
6-1 Test and debug the Invoice Application i need help with this please.. all help is appreciated Source code Java: import java.util.Scanner; import java.text.NumberFormat; public class InvoiceApp { public static void main(String[] args) { Scanner sc = new Scanner(System.in); String choice = "y"; while (!choice.equalsIgnoreCase("n")) { // get the input from the user System.out.print("Enter customer type (r/c): "); String customerType = sc.next(); System.out.print("Enter subtotal: "); double subtotal = sc.nextDouble(); // get the discount percent double discountPercent = 0.0; switch(customerType) {...
How do i write the pseudocode for this java code? First, write out pseudocode, and then create a program to help you by accomplishing the following tasks: : Use command line interface to ask the user to input the following. ○ How many apples are on hand ○ How many apples should be in stock ○ How many oranges are on hand ○ How many oranges should be in stock Perform an operation to determine how many of...
Computer Science 182 Data Structures and Program Design Programming Project #3 – Link List Card Games One of the nice things about Java is it is very easy to do fun things with graphics. The start code provided here will display a deck of cards on the screen and shuffle them. Your mission, is to start with this code and build a card game. Blackjack, poker solitaire, what ever your heart desires. Blackjack is the easiest. Obviously any program you...
#include <iostream> #include <iomanip> #include <vector> using namespace std; Part 1. [30 points] In this part, your program loads a vending machine serving cold drinks. You start with many foods, some are drinks. Your code loads a vending machine from foods, or, it uses water as a default drink. Create class Drink, make an array of drinks, load it and display it. Part 1 steps: [5 points] Create a class called Drink that contains information about a single drink. Provide...
While reading the story, consider the culture (or sub culture) and related communication styles the story reveals. Consider too, possibly, the values, behavioral norms, social practices, social artifacts, etc. After reading the story through the lens of this idea, please compose a full academic length (evidence-based 7 to 11 sentence long) paragraph which addresses the following prompt: What does the story reveal about the culture it portrays and/OR the communication styles the culture shares? In other words, what does the...
Additional code needed: PartA: BurgerOrder class (1 point) Objective: Create a new class that represents an order at a fast-food burger joint. This class will be used in Part B, when we work with a list of orders. As vou work through this part and Part B, draw a UML diagram of each class in using the UML drawing tool 1) Create a new Lab5TestProject project in Netbeans, right-click on the lab5testproject package and select New>Java Class 2) Call your...