Answer_Key_Version_4

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 4 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 4. 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 4 Page 1 of 11
1. What is the type of x ? std::string const a { "CSE232" } ; auto x = a.at(1); (a) char (b) std::string & (c) char const (d) char & (e) std::string const * (f) std::string const (g) char const * (h) std::string const & (i) char const & Correct answers: (a) 2. 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) Dangling Else Problem (b) Inconsistent Capitalization Problem (c) Overloaded Operator Problem (d) Ambiguous Flow Control Problem (e) Unsigned Overflow Problem (f) Under-Documented Problem Correct answers: (a) 3. When will a stream (like cin ) evaluate as false in a conditional expression? (a) When the stream has more characters to process (b) When the stream is waiting to access a file (c) When the stream has extracted a false value (d) Never, a stream is always considered true (e) Always, a stream is always considered false (f) When the stream is in an error state like at End-Of-File. Correct answers: (f) 4. Which of the following is FALSE about ini- tializations? (a) Initialization is necessary for const variables. (b) Initialization (or declaration) needs to be made before a variable is used. (c) Initialization is recommended to avoid undefined behavior. (d) Initialization can use an equal sign, curly brackets or parentheses. (e) Initializations must use a literal value. (f) All of the above are true Correct answers: (e) 5. If a function’s sole purpose is its side-effects, what should its return type be? (a) It depends on if the function performs IO operations. (b) Impossible to determine as all func- tions must return a value. (c) Omitted (d) int (e) It doesn’t matter as the return state- ment won’t have a value. (f) void Correct answers: (f) 6. Accessing the value of an uninitialized char variable will result in what? (a) A segmentation fault (b) The space character ( ’ ’ ) (c) Undefined behavior (d) A null pointer (e) A null character (f) A random character from the ASCII chart (g) A compile-time error Correct answers: (c) Answers Version 4 Page 2 of 11
7. What type must x be for the following code to compile? x = cout << 4; (a) No type exists that x could be as the code above can’t possibly compile (b) std::string (c) An integer type (d) A stream (e) The type auto (f) None of the above Correct answers: (d) 8. 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) Variable names (b) Comments (c) Memory uses (d) Scopes (e) Code sizes (f) Blocks (g) Functions (h) Exceptions Correct answers: (d) 9. What is the type of x? auto x = My Function("abcd"); (a) char (b) int (c) std::string (d) void (e) Impossible to determine with the in- formation given Correct answers: (e) 10. Which of these types do we recommend against using? (a) double (b) long (c) char (d) bool (e) int (f) None of these types are recommended against Correct answers: (b) 11. Which type of pointer should you NOT dereference? (a) A const pointer (b) A pointer to a null character (c) A null pointer (d) A pointer with a memory address (e) A pointer to a const object (f) A pointer to a local variable (g) A pointer that has been assigned (h) A pointer that points at another pointer Correct answers: (c) 12. When writing C++ code how often should you compile (and run) your code? (a) After each comment is written (b) Once per individual program written (c) As often as possible (d) You should never compile/run your code, you should rely on continuous integration (e) After every expression is written (f) After each function is completed Correct answers: (c) Answers Version 4 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. Which of the following is NOT a binary op- erator? (a) = (b) << (c) && (d) += (e) / (f) >= (g) ++ Correct answers: (g) 14. The -r flag for the rm program is short for what? (a) rotating (b) recursive (c) relevant (d) repeat (e) ranked (f) return (g) reverse (h) recent Correct answers: (b) 15. 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) char (c) unsigned long (d) std::string (e) short (f) None of the above Correct answers: (c) 16. Why shouldn’t you return a reference to a local variable? (a) Because references can only be initial- ized, not declared. (b) Because a function’s return type can’t have type modifiers. (c) Wrong, you should return references to local variables. (d) Because local variables will be de- stroyed when they go out of scope. (e) Because making a reference will cause an unnecessary copy to be created. (f) Because local variables must be const and references could alter them. (g) Because it violates the property of ”Independence” Correct answers: (d) 17. What is printed by the following code? char c = ’e’; c++; std::cout << c; (a) d (b) f (c) e1 (d) c (e) 1 (f) e (g) None of the above due to a compila- tion error Correct answers: (b) 18. What is the return type of this function? std::string Exclamation(int num) { return "!"; } (a) int * (b) int (c) Exclamation (d) "!" (e) std::string (f) None of the above Correct answers: (e) Answers Version 4 Page 4 of 11
19. For which values of int x will the following expression be true? -2 < x <= 2 (a) -2, -1, 0, 1, 2 (b) None exist (c) -2, -1, 0, 1 (d) -1, 0, 1, 2 (e) All possible values of x (f) -1, 0, 1 Correct answers: (e) 20. The getline function will read from a stream until what type of character is en- countered? (a) The space character (b) The newline character (c) A whitespace character (include space, newline, and tab) (d) A non-ASCII character (e) A non-alphabetic character Correct answers: (b) 21. Which of the following invokes a function with no arguments provided? (a) Func(void) (b) (Func) (c) Func() (d) Func {} (e) Func(NULL) (f) Func Correct answers: (c) 22. If you have an integer variable that you know can never be negative, what type should you use to store it? (a) unsigned (b) double (c) std::string (d) char (e) long (f) bool (g) int Correct answers: (g) 23. Which of the following is a char literal? (a) a (b) char literal (c) "Z" (d) char (e) *chr (f) ’#’ Correct answers: (f) 24. 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 doing so is a code smell. (c) Because such names will shadow other names in the namespace std (d) Because it makes the variable const (e) Because it violates the language stan- dard. (f) Because it will be confusing to future readers. Correct answers: (e) 25. What does the std::endl object do when passed as the second operand to the inser- tion operator? (a) It indicates that the stream should be prepared for new characters (b) It doesn’t do anything, it is instead used to indicate comments (c) It indicates the End-Of-File (d) It can’t be used with an insertion op- erator (e) It causes the stream to separate words according to whitespace (f) It resets the stream (g) It causes a newline character to be written to the stream Correct answers: (g) Answers Version 4 Page 5 of 11
26. Which of the following can you NOT make a const reference to? (a) A const value (b) A value returned by a function (c) A reference (d) A literal value (e) A non-const value (f) A pointer (g) All of the above permit const refer- ences Correct answers: (g) 27. 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) The function doesn’t conform to the style guide (b) A string can’t be used in a conditional expression (c) Control can reach the end of a non- void function (d) Strings don’t have a substr member function (e) Nothing is wrong with the function Correct answers: (c) 28. What is a pointer’s value? (a) A const reference to another object (b) An address in memory (c) A signed long (d) The value nullptr unless it was ini- tialized or assigned (e) All of the above Correct answers: (b) 29. 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) Fall-through (b) Implicit Execution (c) Assignment (d) Repetition (e) Iteration (f) Linkage (g) Chaining Correct answers: (g) 30. 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) Syntax (c) Independence (d) Legibility (e) Separation (f) Encapsulation (g) Lifetime Correct answers: (f) 31. The std in std::cout is an example of what part of C++? (a) A variable (b) A namespace (c) A function (d) A file (e) An object (f) An exception (g) A using (h) A class Correct answers: (b) Answers Version 4 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
32. 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) 33. 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 runs the source code being interpreted, a compiler gener- ates a new program that can be run later. (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 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. (e) None of the above are true Correct answers: (b) 34. 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) 35. What does this code output? for (unsigned i = 4; i >= 0; --i) { std::cout << i; } (a) 43210 (b) 3210 (c) 4321 (d) 321 (e) None of the above Correct answers: (e) 36. A syntax error is an example of what cate- gory of error? (a) Undefined behavior (b) A user-defined error (c) A run-time error (d) An instrumental error (e) A compile-time error (f) An uncaught exception Correct answers: (e) Answers Version 4 Page 7 of 11
37. 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) 38. In order for file redirection to work, what must a program do? (a) It must be executed by an IDE (b) It must terminate only after the End- Of-File is encountered (c) It must be an executable C++ pro- gram (d) It must be compiled with the -r flag (e) It must have a using directive for the std namespace (f) It must read and/or write to the stan- dard file streams (g) It must conform to the language stan- dard and the style guide Correct answers: (f) 39. 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) 40. Why should you generally not compare floats for equality? (a) Because float arithmetic is imprecise (b) Because floats must be converted to ints before comparison operators can be used (c) Because floats are casted to doubles when used in math (d) Because a float’s value changes each time it is read (e) Because floats don’t support the equality operator (f) All of the above are false, floats should be compared for equality Correct answers: (a) 41. When you increment a pointer, for instance: ++pointer variable; What happens? (a) Nothing happens, because the prefix increment was used, only the value re- turned is affected (b) The value at the pointer’s address is incremented by one (c) The pointer now points to the next address in memory (d) A syntax error is thrown by the com- piler as pointers can’t be incremented (e) Undefined behavior, the language specification doesn’t specify what will happen, so the result is undefined Correct answers: (c) Answers Version 4 Page 8 of 11
42. The continue statement is different from the break statement in one key way, what is it? (a) It is only permitted in while loops, but not in other iterative statements. (b) It requires the use of an if statement. (c) It causes iteration to resume instead of cease. (d) It always creates an infinite loops, unless a break statement is also in- cluded. (e) It is actually identical to break , its use is entirely aesthetic. (f) It is only supported by specific com- pilers and isn’t in the language stan- dard (g) Its use is strongly discouraged due to the poor habits it inspires (h) It can’t be used in blocks due to the ambiguity. Correct answers: (c) 43. 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 random character will be returned (c) A character from another string will be returned (d) Undefined Behavior (e) The last character will be returned (f) A Syntax Error Correct answers: (d) 44. When outputting a double, sometimes it outputs like this: 1.400e-06 What does this mean? (a) It means that the double is stored with some estimated imprecision (b) It means that the buffer is full and needs to be written to the output de- vice (c) It means that you are outputting too many values at once and they are overwriting each other (d) It means 1.4 times 10 raised to the negative 6 (e) It means the value 1.4 combined with error code number 6 (f) None of the above are true Correct answers: (d) 45. 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 4 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
46. 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 byte. (b) It means that a bool’s size is the same as the size of a function. (c) It means that a bool takes up one bit. (d) It means that a bool true value is equivalent to an integer 1. (e) It means that a bool can hold at most one value. (f) It means that a bool takes up one word (on a 64-bit machine this is 64 bits). (g) It means that a bool’s type deter- mines if it is true or false. Correct answers: (a) 47. What arguments is this function being called with? int x = 4; int y = 17; Func(++x, y--); (a) 5 and 16 (b) 4 and 17 (c) 5 and 17 (d) 4 and 16 Correct answers: (c) 48. 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) 49. A newline character is required by the lan- guage standard after which of the following statements? (a) Block statements (b) Preprocessor statements (c) Function Definitions (d) Switch case statements (e) For loop statements (f) None of the above Correct answers: (f) 50. How do you create a null reference? (a) By assigning it the value false (b) By making a reference to a derefer- enced null pointer (c) By using const cast (d) By assigning it the value 0 (e) By subtracting one from a null char- acter (f) By allowing the reference’s lifetime to end (g) None of the above, it is impossible Correct answers: (g) 51. What is the relative path for the current working directory? (a) / (b) . (c) ~ (d) pcwd (e) - (f) .. Correct answers: (b) Answers Version 4 Page 10 of 11
52. 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) 53. What is wrong with this function invoca- tion? std::cout << Func(int x, &y); (a) The ampersand ( & ) doesn’t belong in a function call (b) Func is an invalid name due to the presence of an uppercase letter (c) The namespace for the function isn’t specified (d) The code will result in undefined be- havior due to the lack of assignment (e) Functions can’t be invoked in larger expressions (f) Arguments shouldn’t have a type dec- laration (g) The std::endl was omitted Correct answers: (f) 54. C++ was created to extend which prior programming language? (a) Python (b) Bash (c) C (d) Fortran (e) C+ (f) Basic (g) Haskell (h) None of the above Correct answers: (c) 55. 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) Answers Version 4 Page 11 of 11