Kami+Export+-+HW2

pdf

School

University of Massachusetts, Lowell *

*We aren’t endorsed by this school

Course

COMP 1010

Subject

Computer Science

Date

Jan 9, 2024

Type

pdf

Pages

7

Uploaded by BrigadierScience12941

Report
Computing I: Homework Packet 2 Name___________________________________ SHORT ANSWER. Write the word or phrase that best completes each statement or answers the question. 1) A switch statement variable must be ________. 1) 2) ________ is a type whose values are defined by a list of constants of type int. 2) 3) A loop that iterates one too many or one too few times is said to be ________. 3) 4) A ________ loop always executes the loop body at least once, regardless of the loop condition. 4) 5) The code following the ________ case is executed if none of the other cases are matched in a switch statement. 5) 6) Variables defined inside a set of braces are said to be ________ to that block of code. 6) 7) A ________ expression is an expression that can be thought of as being true or false. 7) 8) The format specifier used to tell scanf to expect a double precision floating point number is a percent sign followed by the character(s) ________. 8) 9) Each repetition of a loop body is called ________. 9) 10) A compound statement that contains variable declarations is called a ________. 10) 11) The compiler always pairs an else with ________. 11) MULTIPLE CHOICE. Choose the one alternative that best completes the statement or answers the question. 12) What is the output of the following code fragment? int i = 5; switch(i) { case 0: i = 15;break; case 1: i = 25;break; case 2: i = 35;break; case 3: i = 40; default: i = 0; } printf("%d\n", i); 12) A) 25 B) 35 C) 0 D) 15 E) 40 1 enum Off by one Do while default local boolean %lf iteration block the nearest if statement C an integer or a character
13) If you need to write a do - while loop that will ask the user to enter a number between 2 and 5 inclusive, and will keep asking until the user enters a correct number, what is the loop condition? 13) A) (2 <= number && number <= 5) B) (2 <= num <= 5) C) (2 > number && number > 5) D) (number < 2 || number > 5) E) (2 < 5 < number) 14) Which of the following symbols has the highest precedence? 14) A) || B) - C) && D) ++ 15) Which of the following data types may be used in a switch statement? 15) A) char B) enum C) int D) long E) all of the above 16) Given the following code, what is the final value of i? int i; for(i = 0; i <= 4;i ++ ) { printf("%d\n", i ); } 16) A) 4 B) 0 C) 3 D) 5 17) What is the output of the following code fragment? int x = 0; { int x = 13; printf("%d,", x); } printf("%d\n", x); 17) A) 0,13 B) 13,13 C) 13,0 D) Nothing, there is a syntax error. 18) Which of the following boolean expressions tests to see if x is between 2 and 15 (including 2 and 15)? 18) A) (x >= 2 && x <= 15) B) (2 <= x || x <= 15) C) (x <= 15 || x >= 2) D) (2 <= x <= 15) 19) What is the value of x after the following code executes? int x = 10; if(x ++ > 10) { x = 13; } 19) A) 13 B) 11 C) 9 D) 10 20) Which loop structure always executes at least once? 20) A) while B) for C) sentinel D) do - while 2 a E D d b a d a
21) What is the value of x after the following code executes? int x = 10; if( ++ x > 10) { x = 13; } 21) A) 13 B) 11 C) 10 D) 9 22) What is wrong with the following for loop? int i; for(i = 0; i < 10; i -- ) { printf("Hello\n"); } 22) A) infinite loop B) off - by - one error C) cannot use a for - loop for this D) i is not initialized. 23) Given the following code, what is the final value of i? int i,j; for(i = 0; i < 4; i ++ ) { for(j = 0; j < 3; j ++ ) { if(i == 2) break; } } 23) A) 0 B) 3 C) 5 D) 4 24) How many times is "Hi" printed to the screen? (There are no typos in this problem.) int i; for(i = 0; i < 14; i ++ ); printf("Hi\n"); 24) A) 15 B) 1 C) 13 D) 14 25) If x is 0, what is the value of the boolean expression (!x == 0)? 25) A) syntax error B) unable to determine C) 1 D) 0 26) What is the value of the following boolean expression? (1 && (4/3 || !(6))) 26) A) 0 B) 1 C) illegal syntax D) false 27) Which of the following is not a good reason for choosing a certain loop control (while, do - while, or for loop for example)? 27) A) the condition for ending the loop B) what the loop does C) if the loop is in a function D) the minimum number of iterations of the loop 3 a b d d a a d
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help
28) Which boolean operation is described by the following table? A B Operation True True True True False True False True True False False False 28) A) and B) or C) not D) none of the above 29) Which of the following are allowed in the third section of the for loop statement? 29) A) i ++ B) printf("Hello\n"); C) i -- D) i += 2 E) all of the above 30) Given the following enumerated data type definition, what is the value of SAT? enum MyType{SUN = 3,MON = 1,TUE = 3,WED,THUR,FRI,SAT,NumDays}; 30) A) 5 B) 8 C) 7 D) 6 E) unknown 31) What is the output of the following code fragment? { int x = 13; printf("%d,", x); } printf("%d\n", x); 31) A) 0,13 B) 13,13 C) 13,0 D) Nothing, there is a syntax error. 32) Which of the following are equivalent to (!(x < 15 && y >= 3))? 32) A) (x > 15 || y < 3) B) (x >= 15 && y < 3) C) (x >= 15 || y < 3) D) (x > 15 && y <= 3) E) B and C 33) What is the output of the following code fragment if x is 15? if(x < 20) if(x < 10) printf("less than 10\n "); else printf("large\n"); 33) A) large B) less than 10 C) nothing D) no output, syntax error 4 a e d c E b
34) If x is 42, what is the value of the boolean expression (!x == 0)? 34) A) 0 B) unable to determine C) syntax error D) 1 35) Given the following enumerated data type definition, what is the value of SAT? enum MyType{SUN,MON,TUE,WED,THUR,FRI,SAT,NumDays}; 35) A) 8 B) 6 C) 7 D) 5 E) unknown 36) Which of the following are valid case statements in a switch? 36) A) case 1: B) case 1.5: C) case x < 4: D) case 'ab': 37) Which boolean operation is described by the following table? A B Operation True True True True False False False True False False False False 37) A) and B) not C) or D) none of the above 38) If a programming language does not use short - circuit evaluation, what is the output of the following code fragment if the value of myInt is 0? int other = 3, myInt; if(myInt ! = 0 && other % myInt ! = 0) printf("other is odd\n"); else printf("other is even\n"); 38) A) 0 B) run - time error, no output C) other is even D) other is odd 39) When testing a program with a loop, which of the following tests should be done? 39) A) one less than the maximum number of iterations B) no iterations of the loops C) the maximum number of iterations D) one more than the maximum number of iterations E) A, B, and C 40) Which of the following data types can be used in a switch controlling expression? 40) A) char B) int C) double D) enum E) A, B, and D 5 E E b A A c a
41) If you want a loop to quit iterating if x < 10 and y > 3, what would be the proper loop condition test? 41) A) (x > 10 || y < 3) B) (x >= 10 && y <= 3) C) (x < 10 && y > 3) D) (x >= 10 || y <= 3) 42) What is wrong with the following switch statement? int ans; printf("Type y for yes on n for no\n"); scanf("%d", &ans); switch (ans) { case 'y': case 'Y': printf("You said yes\n"); break; case 'n': case 'N':printf("You said no\n"); break; default: printf("invalid answer\n"); } 42) A) There are no break statements on 2 cases. B) ans is an int. C) break; is illegal syntax. D) nothing TRUE/FALSE. Write 'T' if the statement is true and 'F' if the statement is false. 43) All nested if - else statements can be converted into switch statements. 43) 44) All switch statements can be converted into nested if - else statements. 44) 45) A break statement in a switch stops your program. 45) 46) A semicolon by itself is a valid C statement. 46) 47) The break statement causes all loops to exit. 47) 48) In an enumerated data type, different constants may not have the same value. 48) 49) It is illegal to make function calls inside a switch statement. 49) 50) A boolean expression may evaluate to more than 2 values. 50) Sample Exam Questions and Extra Practice (Canning Problems). You do not have to turn in solutions to these questions. 51) Canning Problem 18: Your task is to write a C program that accepts two positive integer values from the keyboard, say L and H. Both L and H will be less than 21. Your program then prints out a solid box of asterisks, with L horizontal stars and H vertical stars. The box is filled with asterisks. For example, If you were to type in 5 and 3, your output would be: ***** ***** ***** 6 F T F T F F F T b C
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help
52) Write a complete program that will ask the user for two integers and the sum of those two integers. It will then print "Correct!" on the screen if their sum was correct and "Incorrect" otherwise. The program should continue asking for new sets of integers for as long as the user wishes. 53) Canning Problem 19: In grade school you learned that the area of a rectangle is its length times its height. Write a C program that inputs two floating point numbers from the keyboard that represent the length and the height of a rectangle. Your program should output the area of the rectangle. Your variables should be of type double. 54) Write a complete program that will ask the user for the current temperature. The program should print a message on the screen indicating that it is too hot if the temperature is above 80, too cold if it is below 60 and just right if the temperature is in the middle. Can this program be done using a switch statement, why or why not? 7