Download CSE 240 Midterm Exam With 100% Correct Answers 2023 and more Exams Advanced Education in PDF only on Docsity! CSE 240 Midterm Exam With 100% Correct Answers 2023 What computing paradigm enforces stateless (no side-effects) programming? - Correct Answer-Functional What programming paradigm does Fortran belong to? - Correct Answer-Imperative What computing paradigm can solve a problem by describing the requirements, without writing code in a step-wise fashion to solve the problem? - Correct Answer-Logic What programming language characteristics impact the readability of the programs written in this language? - Correct Answer-Data Structures, Syntax Design, and Control Structures What is the major improvement of structured programming languages over the earlier programming languages? - Correct Answer-Removing Goto statement from the language. If your program was designed to print "Hello World" ten (10) times, but during execution, it printed eleven (11) times. What type of error is it? - Correct Answer-Semantics Error For the following BNF ruleset, which are terminal symbols? Select all that apply. <char> ::= a | b | c | ... | x | y | z <identifier> ::= <char> | <char> <identifier> - Correct Answer-y and a What does the | (pipe) symbol in a BNF rule mean? - Correct Answer-It is an "or" operation. Choose one of the options. Can the identifier (a string) "base_variable" be created using the following BNF rules? <char> ::= a | b | c | ... | s | ... | x | y | z <identifier> ::= <char> | <char> <identifier> - Correct Answer-No - there is an underscore in the identifier name that cannot be generated. If a program contains an error that divides a number by zero at the execution time. This error is typically a - Correct Answer-Semantic error During compilation, linker is used for ___________________. - Correct Answer- resolving external references (bring in code from other libraries). If you like to see accurate debugging information, which of the following program processing processing mechanism would you recommend? - Correct Answer- Interpretation Macros-Processing in C (or C++) takes place during which specific phase? - Correct Answer-Pre-processing Which implementation of a function has potentially the best performance in terms of execution speed? - Correct Answer-Macro Given the following code, what is the expected value for z? Assume that you are using a compiler that doesn't do any special treatment of macro parameters. #include <stdio.h> #define func(x, y) (x > y) ? y : x int main() { int x = 10;int y = 9;int z = func(++x, y++); } - Correct Answer-10 Converting an integer value 5 to a float number 5.0 takes - Correct Answer-At least one important machine instruction to perform conversion between data binary formats. Explicit type conversion is commonly refer to as __________ . - Correct Answer-Casting Which of the following statements are correct if a language is strongly typed. Select all that apply. - Correct Answer-Each name in a program has a single type associated with it and type errors are always reported. Type checking happens during compilation phase. It reinforces which of the following structural layers? - Correct Answer-Contextual The imperative programming paradigm is popular and is a part of object-oriented computing paradigm, because it ____ (Select all answers that apply). - Correct Answer- Matches the common culture of doing things by following the step-wise instructions and is based on computer organization and thus is efficient to execute on hardware. In C, what function can be used for inputting a string containing spaces? - Correct Answer-fgets(); The forward declaration of a function requires: (Select all answers that apply) - Correct Answer-parameter types, return type, and function name Where is the main() function located in a C++ program? - Correct Answer-Outside any class Given the following definition and declarations: #define size1 10 const int size2 = 20; char a1[size1]; char a2[size2]; which line is most likely to cause a compilation error? - Correct Answer-char a2[size2]; Given the following snippet of code, answer the following two questions based on the code: typedef enum {Sun, Mon, Tue, Wed, Thu, Fri, Sat} days; days x = Mon, y = Sat; while (x != y) { x++; } y++; printf("x = %d, y = %d", x, y); 1. What value will be printed for variable x? 6 . 2. What value will be printed for variable y? 7 (Hint: enums are really just integers...) - Correct Answer-1. 6 2. 7 Consider the following snippet of code in a 32-bit computer #define MAX 10 struct contact { char name[30]; char email[30]; int phone; ); struct contact A[MAX]; int tail = 0; Which statement can read a phone number into the phone field of the structure? - Correct Answer-scanf("%d", &contactbook[tail].phone); When using an array of structures to store a collection, we typically have a variable storing the number of entries (called something like: tail, count, or size). In addition to the number of entries, what does this variable help to indicate? The tail variable in an array of structures is used for indicating (Select all that apply) - Correct Answer-Where to insert a new item, if the items in the array do not need to be sorted and how far the search() function should go in searching the items of the array. The size (number of bytes) of a structure-type variable can be changed by the following factors. Select all that apply. - Correct Answer-Changing the orders of the members in the structure, adding a member into the structure, and changing the computer from a 32-bit to a 64-bit processor. Consider the following snippet of code on a 32-bit computer: struct contact { char name[30]; int phone; char email[30]; } x; What is the size of variable x in bytes? (x is just a variable containing a struct contact) - Correct Answer-68 When will the buffer be created? - Correct Answer-When the file operation fopen is performed. The reason that we use a buffer between the disk and the memory is _______ - Correct Answer-Disk access speed is low, but large blocks of data can be read from and written to the disk. What parameters are required when performing file operations fread and fwrite? Select all that apply. - Correct Answer-Destination, source, number of items, and item size The reason that we need to call fflush() or cin.ignore() is because the previous - Correct Answer-input leaves a character in the buffer. Assume that the search function of a linked list is specified by struct Terminal* search(); What values can the search function return? Select all correct answers. - Correct Answer-The address of a terminal node and 0. Assume this is a 32-bit environment, what is the size of x? (HINT: Don't forget about padding.) struct Terminal { char name[30]; char location[32]; struct Terminal* next; } struct Terminal x; - Correct Answer-68 bytes Given the information below, how would you access the name for a terminal node pointed to by x? struct Terminal { char name[30]; char location[32]; struct Terminal* next; }; struct Terminal *x; - Correct Answer-x->name; Assume that you want to delete the entire linked list pointed to by head. Which of the following deletion operation will cause the most garbage of memory? - Correct Answer- head = NULL; Given the information below, which of the following snippet of codes will insert a new node in the second place in the linked list. Note that name and location won't be set up - the code is intended to only add a new node. Assume that the head variable will store the first node in the linked list and that it already contains at least one node. struct Terminal { char name[30]; char location[32]; struct Terminal* next; } *head, *p, *q; - Correct Answer-p = (struct contact *) malloc(sizeof(struct contact)); p->next = head->next; head->next = p; Given this snippet of codes, what is the expected output? #include <stdio.h> char *getString(char *str) { return str; } void main() { char *p, q[] = "Hello", *s = "World"; p = getString(s); printf("%s %s\n", p, q); } - Correct Answer-World Hello Given this snippet of codes, identify the passing mechanism used for x (in func). void func(int *x, int y){ *x = *x + y; y = 2; } - Correct Answer-pass-by-address Which of the following statements is true? Using the principle of information hiding in OOP, in an array-implemented queue class, what members should be declared as "public" members? - Correct Answer-enqueue and dequeue functions What are the key features of object orientation in programming languages? Select all that apply. - Correct Answer-Encapsulation of state and dynamic memory allocation If a function calls another function, the local variables in these two functions use the memory from - Correct Answer-different stack frames. Which C/C++ operations will acquire memory from heap? Select all that apply. - Correct Answer-malloc and new What is the key difference between a static variable and a global variable? - Correct Answer-They have different visibility. You do not need to garbage-collect the memory, if it comes from (Select all that apply) - Correct Answer-stack memory, global memory, and static memory Given the snippet of code: int x = 5; int bar(int j) { int *k = 0, m = 5; k = &m; return (j+m); } void main(void) { static int i =0; i++; i = bar(i) + x; } Which variables obtain their memory from the stack? Select all that apply. - Correct Answer-k, m, and j What is the best way of deleting all the nodes in a binary tree pointed to by the root pointer? - Correct Answer-Traverse the tree and delete each node individually. What is the best way of deleting a linked list of objects in C++? - Correct Answer-Use a loop to delete every object in the linked list. A piece of memory must be explicitly garbage-collected, if it comes from - Correct Answer-heap What memory must be garbage-collected by a destructor? - Correct Answer-heap memory created in the constructors in the same class What is the best way of deleting an array created by "p = new StructType[size];" in C+ +? - Correct Answer-delete[] p; We need to write a destructor for a class, if - Correct Answer-heap memory is used in the constructor of the class. Consider a path from the root to a leaf of a class tree based on the inheritance. Which class has the most class members? - Correct Answer-The class at the leaf of the tree. The semantics of multiple inheritance becomes complex and error prone, if the base classes have - Correct Answer-overlapped members. Defining a virtual function in class means that the function - Correct Answer-can be redefined in its child classes. If A is the base class and B is a class derived from A, and x and y are pointers to objects of A and B, respectively, which assignment statement can pass the compiler check? - Correct Answer-x = y; If you declare a pointer to the object of the parent class and use the pointer to access the object of a child class, you can access - Correct Answer-the members that exist in the parent class. Given the following class definition and the variable declaration: class employee { public:char *name;long id;}; class manager { public:employee empl;char* rank;} x; Which of the following assignment statement is correct? - Correct Answer-x.empl.id = 12345; If the relation between two C++ classes can be best described as "is-a" relation, we should - Correct Answer-derive one class from the other (inheritance). What type casting mechanism should be used with caution as it may truncate a larger object to a smaller one while casting an object of one class to an object of a different class? - Correct Answer-reinterpret_cast What type casting mechanism should be used if you want to cast an integer value to a double value? - Correct Answer-static_cast Consider C++'s typing system. C++ uses (Select all correct answers): value semantics for its primitive types (such as integer and float) only. both value semantics and reference semantics for its primitive types . reference semantics for its object types only. both value semantics and reference semantics for its object types. - Correct Answer- both value semantics and reference semantics for its primitive types . both value semantics and reference semantics for its object types. It does not cause memory (storage) overhead to create multiple - Correct Answer- names (alias) for a variable. Given the following snippet of C++ code: string name = "Hello"; ofstream myFile; myFile.open(myFile); myFile << name; myFile.close(); What does it do? - Correct Answer-It saves the word Hello into a file in the file system. A throw statement is associated with a specific exception handler through the - Correct Answer-the data type implied by the throw value. What type of values can a throw-statement throw (return)? - Correct Answer-Primitive type, string type, and object types. The try-throw-catch exception handling mechanism is at the level of - Correct Answer- user program.