4.8. Chapter Review
Note that the course policy is that you should not use generative AI without authorization. If you are suspected to have used generative AI and not able to explain/reproduce your work when requested, all your related assignments throughout the semester will be regraded as 0.
Prepare this assignment using a Word document.
Number and write answers under each question.
Paste code screenshots when required.
Use a Word document to paste your screenshots of your code and the results of execution. Number the questions and annotate your answers, when appropriate, to show your understanding.
Which of these are Boolean expressions? Assume the variables are of type
int
:true "false" x = 3 n < 10 count == 22 x <= 2 || x > 10 x == 2 || 3 1 < y < 10
What are the values of these expressions? Be able to explain:
2 < 3 && 4 < 5 2 < 3 && 4 < 3 2 < 3 || 4 < 5 2 < 3 || 4 < 3 3 < 2 || 4 < 3 2 < 3 || 4 < 5 && 4 < 3
Assume
IsBig(x)
returns a Boolean value. Remove the redundant part of this statement:if (IsBig(x) == true) x = 3;
Add parentheses in
2 < 3 || 4 < 5 && 4 < 3
to get a different result.What is printed when the following code executes and x has been set to 0 and y to 3?:
if (x > 0 && (y / x) == 3) { Console.WriteLine("first case"); } else { Console.WriteLine("second case"); }
What does the following code print when x has been set to 2000?:
if (x < 0) { Console.WriteLine("x is negative"); } else if (x == 0) { Console.WriteLine("x is zero"); } else { Console.WriteLine("x is positive"); }
Assume
x
andy
are localint
variables. Code fragments are separated by a blank line below. Pairs of the fragments are logically equivalent, but not necessarily with a directly adjacent fragment. Match the pairs. Be sure you understand when different pairs would behave differently. Caution: there is some pretty awful code here, that we would hope you would never write, but you might need to correct/read! In each equivalent pair, which code fragment is more professional?if (x > 7) { //a x = 5; } y = 1; if (x > 7) { //b x = 5; y = 1; } if (x > 7) //c x = 5; y = 1; if (x > 7) { //d x = 5; } else { y = 1; } if (x > 7) //e x = 5; else if (x <= 7) { y = 1; } if (x > 7) { //f y = 1; } if (x > 7) { x = 5; }
Same situation as the last problem, and same caution, except this time assume the fragments appear in a function that returns an
int
. In each pair of equivalent fragments, which is your preference?y = 1; //a if (x > 7) { return x; } if (x > 7) { //b return x; } y = 1; if (x > 7) { //c return x; } else { y = 1; } if (x > 7) { //d return x; y = 1; } if (x > 7) { //e y = 1; return x; } y = 1; if (x > 7) { //f return x; } if (x > 7); //g return x; return x; //h
Same situation as the last problem, and same caution:
if (x > 5) //a if (x > 7) return x; else y = 1; if (x > 5) { //b if (x > 7) return x; } else { y = 1; } if (x > 7) //c return x; if (x <= 5) y = 1; if (x > 7) //d return x; if (x > 5) y = 1;
The following code should print x is greater than 0. However, the code has errors. Fix the code so that it compiles and runs correctly. Run the program several times with different values of x to test both if and else branches.:
public class Test1 { public static void Main(String[] args) { int x = 3; if (x > 0 Console.WriteLine("x is greater than 0") else Console.WriteLine(x is less than or equal 0"); } }
Copy the code to your Word file and mark your changes in red bold font.
You use either csharprepl or VS Code to test the code.