Answer_Key_Version_3

pdf

School

Michigan State University *

*We aren’t endorsed by this school

Course

232

Subject

Computer Science

Date

Jan 9, 2024

Type

pdf

Pages

11

Uploaded by BailiffHawk7826

Report
Exam 1 CSE 232 (Introduction to Programming II) Fall 2023 ANSWERS VERSION 3 Full Name: . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . PID: . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Instructions: DO NOT START/OPEN THE EXAM UNTIL TOLD TO DO SO. You may however write and bubble in your name, PID and VERSION/FORM NUMBER (with a #2 pencil) on the front of the printed exam and bubble sheet prior to the exam start. This exam is Answers Version 3. Present your MSU ID (or other photo ID) when returning your bubble sheet and printed exam. Only choose one option for each question. Assume any needed #include s and using std::...; namespace declarations are performed for the code samples. Every question is worth the same amount of points. There are 55 questions, but you only need 50 questions correct for a perfect score. No electronics are allowed to be used or worn during the exam. This means smart-watches, phones and headphones need to be placed away in your bag. The exam is open note, meaning that any paper material (notes, slides, prior exams, assignments, books, etc.) are all allowed. Please place all such material on your desk prior to the start of the exam, (so you won’t need to rummage in your bag during the exam). If you have any questions during the exam, please raise your hand and a proctor will assist you. http://xkcd.com/499/ Answers Version 3 Page 1 of 11
1. Which of the following invokes a function with no arguments provided? (a) Func {} (b) Func (c) Func(NULL) (d) Func(void) (e) Func() (f) (Func) Correct answers: (e) 2. Which of the following can you NOT make a const reference to? (a) A reference (b) A value returned by a function (c) A pointer (d) A const value (e) A non-const value (f) A literal value (g) All of the above permit const refer- ences Correct answers: (g) 3. C++ was created to extend which prior programming language? (a) Basic (b) C+ (c) Haskell (d) Bash (e) Fortran (f) C (g) Python (h) None of the above Correct answers: (f) 4. The following code is an example of what problem? if (is file open) if (data is full && process) Turn On Alarm(4); else Close File(); (a) Unsigned Overflow Problem (b) Overloaded Operator Problem (c) Dangling Else Problem (d) Inconsistent Capitalization Problem (e) Ambiguous Flow Control Problem (f) Under-Documented Problem Correct answers: (c) 5. What does this code output? for (unsigned i = 4; i >= 0; --i) { std::cout << i; } (a) 4321 (b) 43210 (c) 3210 (d) 321 (e) None of the above Correct answers: (e) 6. How many iterations of the while loop will occur? while (7) { int x = 4; ++x; if (x > 6) { break; } } (a) 0 iterations (b) 1 iteration (c) 2 iterations (d) 4 iterations (e) 6 iterations (f) 7 iterations (g) more than 13 iterations Correct answers: (g) Answers Version 3 Page 2 of 11
7. What is the return type of this function? std::string Exclamation(int num) { return "!"; } (a) std::string (b) Exclamation (c) int * (d) "!" (e) int (f) None of the above Correct answers: (a) 8. What happens if you index beyond the end of a string. Example: std::string s { "abc" } ; std::cout << s[3]; (a) A Run-Time Error (b) A Syntax Error (c) A random character will be returned (d) Undefined Behavior (e) A character from another string will be returned (f) The last character will be returned Correct answers: (d) 9. On my system, when I call sizeof(bool); I get the value 1 . What does this mean? (a) It means that a bool takes up one word (on a 64-bit machine this is 64 bits). (b) It means that a bool takes up one byte. (c) It means that a bool can hold at most one value. (d) It means that a bool takes up one bit. (e) It means that a bool’s size is the same as the size of a function. (f) It means that a bool true value is equivalent to an integer 1. (g) It means that a bool’s type deter- mines if it is true or false. Correct answers: (b) 10. The getline function will read from a stream until what type of character is en- countered? (a) A whitespace character (include space, newline, and tab) (b) The space character (c) A non-alphabetic character (d) A non-ASCII character (e) The newline character Correct answers: (e) 11. Declaring all local variables at the begin- ning of a function is bad practice because it results in unnecessarily large XXXXs. What term should replace XXXXs? (a) Functions (b) Code sizes (c) Memory uses (d) Scopes (e) Comments (f) Blocks (g) Exceptions (h) Variable names Correct answers: (d) 12. Why should you generally not compare floats for equality? (a) Because floats must be converted to ints before comparison operators can be used (b) Because float arithmetic is imprecise (c) Because floats don’t support the equality operator (d) Because a float’s value changes each time it is read (e) Because floats are casted to doubles when used in math (f) All of the above are false, floats should be compared for equality Correct answers: (b) Answers Version 3 Page 3 of 11
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
13. What is the output of the following code? std::cout << static cast<int>(’b’); (a) 0 (b) 1 (c) 2 (d) 3 (e) 4 (f) None of the above Correct answers: (f) 14. Which of the following is NOT a binary op- erator? (a) && (b) << (c) ++ (d) += (e) = (f) / (g) >= Correct answers: (c) 15. After the code below executes, which pointers have the same value as a ? int x = 67; int y = 34; int * a = &x; int * b = &y; int * c = b; int * d = &y; *c = 67; *b = *a; d = a; *d = 34; (a) b (b) c (c) d (d) Both b and c (e) All of b , c , and d (f) None of them have the same value as a (g) The code is invalid, and thus no an- swer can be given Correct answers: (c) 16. What type must x be for the following code to compile? x = cout << 4; (a) std::string (b) An integer type (c) A stream (d) No type exists that x could be as the code above can’t possibly compile (e) The type auto (f) None of the above Correct answers: (c) 17. The continue statement is different from the break statement in one key way, what is it? (a) It is actually identical to break , its use is entirely aesthetic. (b) It can’t be used in blocks due to the ambiguity. (c) It is only supported by specific com- pilers and isn’t in the language stan- dard (d) It requires the use of an if statement. (e) It causes iteration to resume instead of cease. (f) Its use is strongly discouraged due to the poor habits it inspires (g) It always creates an infinite loops, unless a break statement is also in- cluded. (h) It is only permitted in while loops, but not in other iterative statements. Correct answers: (e) Answers Version 3 Page 4 of 11
18. The language feature of using the same op- erator many times in a single statement, for example: apple = banana = carrot; or cin >> apple >> banana >> carrot; Is called what? (a) Chaining (b) Assignment (c) Iteration (d) Implicit Execution (e) Linkage (f) Fall-through (g) Repetition Correct answers: (a) 19. What is a pointer’s value? (a) A signed long (b) A const reference to another object (c) An address in memory (d) The value nullptr unless it was ini- tialized or assigned (e) All of the above Correct answers: (c) 20. What is wrong with this function invoca- tion? std::cout << Func(int x, &y); (a) Functions can’t be invoked in larger expressions (b) The code will result in undefined be- havior due to the lack of assignment (c) The ampersand ( & ) doesn’t belong in a function call (d) Func is an invalid name due to the presence of an uppercase letter (e) Arguments shouldn’t have a type dec- laration (f) The std::endl was omitted (g) The namespace for the function isn’t specified Correct answers: (e) 21. Which of the following is a char literal? (a) "Z" (b) a (c) ’#’ (d) *chr (e) char (f) char literal Correct answers: (c) 22. When writing C++ code how often should you compile (and run) your code? (a) As often as possible (b) After each function is completed (c) After each comment is written (d) You should never compile/run your code, you should rely on continuous integration (e) After every expression is written (f) Once per individual program written Correct answers: (a) 23. What does the std::endl object do when passed as the second operand to the inser- tion operator? (a) It indicates the End-Of-File (b) It doesn’t do anything, it is instead used to indicate comments (c) It resets the stream (d) It causes the stream to separate words according to whitespace (e) It indicates that the stream should be prepared for new characters (f) It can’t be used with an insertion op- erator (g) It causes a newline character to be written to the stream Correct answers: (g) Answers Version 3 Page 5 of 11
24. What is the relative path for the current working directory? (a) .. (b) pcwd (c) . (d) / (e) ~ (f) - Correct answers: (c) 25. What is wrong with the following function? bool Func(std::string s) { while (!s.empty()) { if (s.at(0) == ’a’) { return true; } s = s.substr(1); } } (a) Control can reach the end of a non- void function (b) The function doesn’t conform to the style guide (c) A string can’t be used in a conditional expression (d) Strings don’t have a substr member function (e) Nothing is wrong with the function Correct answers: (a) 26. In order for file redirection to work, what must a program do? (a) It must be compiled with the -r flag (b) It must have a using directive for the std namespace (c) It must read and/or write to the stan- dard file streams (d) It must conform to the language stan- dard and the style guide (e) It must be an executable C++ pro- gram (f) It must terminate only after the End- Of-File is encountered (g) It must be executed by an IDE Correct answers: (c) 27. What is printed by the following code? char c = ’e’; c++; std::cout << c; (a) d (b) e (c) f (d) e1 (e) 1 (f) c (g) None of the above due to a compila- tion error Correct answers: (c) 28. When will a stream (like cin ) evaluate as false in a conditional expression? (a) When the stream has extracted a false value (b) When the stream is waiting to access a file (c) Always, a stream is always considered false (d) Never, a stream is always considered true (e) When the stream is in an error state like at End-Of-File. (f) When the stream has more characters to process Correct answers: (e) 29. How many bytes are needed to compose one long long ? (a) 1 (b) 2 (c) 4 (d) 8 (e) It depends on the compiler/hardware Correct answers: (e) Answers Version 3 Page 6 of 11
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
30. When you increment a pointer, for instance: ++pointer variable; What happens? (a) Undefined behavior, the language specification doesn’t specify what will happen, so the result is undefined (b) Nothing happens, because the prefix increment was used, only the value re- turned is affected (c) The value at the pointer’s address is incremented by one (d) The pointer now points to the next address in memory (e) A syntax error is thrown by the com- piler as pointers can’t be incremented Correct answers: (d) 31. The -r flag for the rm program is short for what? (a) recursive (b) reverse (c) rotating (d) ranked (e) return (f) relevant (g) recent (h) repeat Correct answers: (a) 32. If you have an integer variable that you know can never be negative, what type should you use to store it? (a) int (b) double (c) std::string (d) char (e) bool (f) long (g) unsigned Correct answers: (a) 33. When outputting a double, sometimes it outputs like this: 1.400e-06 What does this mean? (a) It means that you are outputting too many values at once and they are overwriting each other (b) It means the value 1.4 combined with error code number 6 (c) It means that the double is stored with some estimated imprecision (d) It means that the buffer is full and needs to be written to the output de- vice (e) It means 1.4 times 10 raised to the negative 6 (f) None of the above are true Correct answers: (e) 34. What is the value of x ? std::string s { "abcde" } ; int x = static cast<int>(s.size()); (a) 5 (b) 6 (c) 7 (d) int (e) unsigned int (f) Impossible to determine with the in- formation given Correct answers: (a) 35. What is the type of x ? std::string const a { "CSE232" } ; auto x = a.at(1); (a) char const (b) std::string const & (c) char const * (d) char const & (e) char & (f) std::string & (g) std::string const * (h) std::string const (i) char Correct answers: (i) Answers Version 3 Page 7 of 11
36. What is the term that describes the fact that one function’s local variables can’t be directly altered by other functions? (a) Locality (b) Encapsulation (c) Lifetime (d) Separation (e) Independence (f) Syntax (g) Legibility Correct answers: (b) 37. Which of these types do we recommend against using? (a) double (b) long (c) char (d) int (e) bool (f) None of these types are recommended against Correct answers: (b) 38. What arguments is this function being called with? int x = 4; int y = 17; Func(++x, y--); (a) 5 and 17 (b) 4 and 17 (c) 4 and 16 (d) 5 and 16 Correct answers: (a) 39. If a function’s sole purpose is its side-effects, what should its return type be? (a) void (b) int (c) It doesn’t matter as the return state- ment won’t have a value. (d) It depends on if the function performs IO operations. (e) Impossible to determine as all func- tions must return a value. (f) Omitted Correct answers: (a) 40. A newline character is required by the lan- guage standard after which of the following statements? (a) For loop statements (b) Preprocessor statements (c) Function Definitions (d) Block statements (e) Switch case statements (f) None of the above Correct answers: (f) 41. Most C++ fundamental types wrap around when they overflow/underflow. But which of the following types does the C++ stan- dard guarantee will have this behavior? (a) int (b) std::string (c) char (d) unsigned long (e) short (f) None of the above Correct answers: (d) 42. Accessing the value of an uninitialized char variable will result in what? (a) A null pointer (b) A segmentation fault (c) A compile-time error (d) Undefined behavior (e) A null character (f) A random character from the ASCII chart (g) The space character ( ’ ’ ) Correct answers: (d) Answers Version 3 Page 8 of 11
43. Why shouldn’t you return a reference to a local variable? (a) Wrong, you should return references to local variables. (b) Because references can only be initial- ized, not declared. (c) Because local variables must be const and references could alter them. (d) Because local variables will be de- stroyed when they go out of scope. (e) Because it violates the property of ”Independence” (f) Because a function’s return type can’t have type modifiers. (g) Because making a reference will cause an unnecessary copy to be created. Correct answers: (d) 44. Which type of pointer should you NOT dereference? (a) A pointer to a const object (b) A pointer to a local variable (c) A pointer that points at another pointer (d) A null pointer (e) A pointer that has been assigned (f) A const pointer (g) A pointer with a memory address (h) A pointer to a null character Correct answers: (d) 45. What is the distinction between an inter- preter and a compiler? (a) An interpreter can only interpret some languages (like Python), but compilers can compile all possible lan- guages. (b) An interpreter interprets the code and makes decisions about ambiguous code in the manner that the developer most likely intended, a compiler will instead halt and raise an error if am- biguous code is encountered. (c) An interpreter isn’t guaranteed to al- ways do the same thing when pre- sented with the same source code, a compiler must always have determin- istic behavior. (d) An interpreter runs the source code being interpreted, a compiler gener- ates a new program that can be run later. (e) None of the above are true Correct answers: (d) 46. The std in std::cout is an example of what part of C++? (a) An exception (b) A file (c) A class (d) An object (e) A namespace (f) A function (g) A variable (h) A using Correct answers: (e) Answers Version 3 Page 9 of 11
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
47. For what values of x will this program have a ’b’ character in the output? switch (x + 1) { case 0: case 1: std::cout << ’a’; case 2: std::cout << ’b’; default: std::cout << ’c’; } (a) -1 (b) 0 (c) 1 (d) 2 (e) 3 (f) -1 , 0 , and 1 (g) 0 , 1 , and 2 (h) -1 , 1 , 2 , and 3 (i) All possible values of x Correct answers: (f) 48. Which of the following is FALSE about ini- tializations? (a) Initializations must use a literal value. (b) Initialization is recommended to avoid undefined behavior. (c) Initialization is necessary for const variables. (d) Initialization (or declaration) needs to be made before a variable is used. (e) Initialization can use an equal sign, curly brackets or parentheses. (f) All of the above are true Correct answers: (a) 49. For which values of int x will the following expression be true? -2 < x <= 2 (a) -1, 0, 1 (b) All possible values of x (c) -1, 0, 1, 2 (d) None exist (e) -2, -1, 0, 1, 2 (f) -2, -1, 0, 1 Correct answers: (b) 50. What is the type of x? auto x = My Function("abcd"); (a) std::string (b) int (c) char (d) void (e) Impossible to determine with the in- formation given Correct answers: (e) 51. When will the body of the if statement ex- ecute? if (x || y) { ... } (a) Only when x is true (b) Only when y is true (c) Only when x and y are both true (d) Only when one of either x or y are true (e) Only when x and/or y are true (f) Impossible to determine with the in- formation given Correct answers: (e) 52. How do you make a variable that can have multiple types? (a) By using decltype (b) By using static cast (c) By declaring its type as auto (d) By using typedef (e) It is impossible Correct answers: (e) Answers Version 3 Page 10 of 11
53. Why shouldn’t you start the name of a vari- able with a digit? (a) Because doing so will result in unde- fined behavior. (b) Because such names will shadow other names in the namespace std (c) Because it violates the language stan- dard. (d) Because doing so is a code smell. (e) Because it makes the variable const (f) Because it will be confusing to future readers. Correct answers: (c) 54. A syntax error is an example of what cate- gory of error? (a) Undefined behavior (b) A run-time error (c) A user-defined error (d) A compile-time error (e) An uncaught exception (f) An instrumental error Correct answers: (d) 55. How do you create a null reference? (a) By allowing the reference’s lifetime to end (b) By making a reference to a derefer- enced null pointer (c) By assigning it the value false (d) By subtracting one from a null char- acter (e) By using const cast (f) By assigning it the value 0 (g) None of the above, it is impossible Correct answers: (g) Answers Version 3 Page 11 of 11