CSE 2451 - Adv. C++ Programming Final Review

¡Supera tus tareas y exámenes ahora con Quizwiz!

Variables

- Named memory locations that we can use to store data - The storage area in a snippet of code where the program can easily manipulate (using the names weassign to the variable)

Characteristics of C

- Procedural language (vs object-oriented language)• - Free-form language: the position of characters on the page in programming text is insignificant - No garbage collection (manual memory management) - Direct memory access - Compiled language (vs interpreted language)• - C can run on bare machine/meta

terminal command to run the resulted executable program file

./myprogram

what's the value of x after the following assignment operation? int x = 120 / (5 * 12) / 4;

0

what's the value of y after the following code snippet is executed? int x = 0, y = 1; x = (x < 0) && (++y);

1

what's the value of y after the following code snippet is executed? int x = 0, y = 1; x = (x < 0) || (y++);

2

what's the value of x after the following assignment operation? int x = 120 / 5 * 12 / 4;

72

What does the following declaration declare? int (*foo)(double * x, int y); A. a pointer foo to a function that takes 1) a pointer to a variable of double type and 2) a variable of int type, and returns a value of int type B. a function foo, which takes 1) a pointer to a variable of double type 2) a variable of int type, and returns a value of int type C. a pointer foo to a function that takes 1) a variable of double type and 2) a variable of int type, and returns a value of int type D. a pointer foo to a variable of int type

A

Which of the following statements about an array is NOT true? a. an array can store its elements in non-consecutive segments in the program's address space b. an array decays to a pointer to its 1st element when being passed as an input argument to a function c. one can use sizeof(ham)/sizeof(ham[0]) to infer the number of elements stored in ham, a 1-d array D. an array will not decay to a pointer when used as an operand of sizeof operator

A

For the following declaration of an enumerated type, fill in the values for the enumerators: enum bar {A, B, C=5, D, E=6, F}; A = B = D = F =

A = 0 B = 1 D = 6 F = 7

I have a function of the following prototype, which is the correct function pointer to this function? char * bar(int x, double y); A. char *fptr(int x, double y); B. char * (*fptr)(int, double); C. char * (**fptr)(int x, double y); D. char (*fptr)(int x, double y);

B

What does -DEBUG option flag do when you add it to compile your source code, e.g., in the following command: gcc -Wall -DEBUG src.c -std=c99 A. it instructs gcc to insert symbolic information in the excutable file for downstream tools to use, e.g., debugging with gdb or valgrind B. it defines a macro with EBUG as its name, and this macro is not tied to anything piece of code C. it defines a macro with DEBUG as its name, and this macro is not tied to anything piece of code D. it doesn't do anything

B

Which of the following statement about members of a struct is NOT true? A. a struct can have array of constant known size as its member B. a struct can have just one member, and it can be a flexible array C. a struct can have other struct members as long as they are not the current struct type D. a struct can have at most one flexible array as its member

B

Which of the following usage of typedef defines an alias (func_ptr_type_alias) for the following function pointer type: /* a function pointer to a function that takes 1) an int variable 2) a double variable and 3) a char variable, and returns a void pointer */ void * (*fptr)(int a, double b, char c); A. typedef func_ptr_type_alias void* (* ) (int, double, char); B. typedef void * (*func_ptr_type_alias)(int, double, char); C. typedef void * (int, double, char) func_ptr_type_alias; D. typedef void * (* )(int, double, char) func_ptr_type_alias;

B

Which of the following statement is NOT true about the function call stack? A. the function call stack locates in the stack segment inside a program's memory space. B. the function call stack will create a stack frame for each function C. local variables within each function call are stored in the respective stack frame D. the function call stack is a dynamic data structure with a First In Last Out processing order

B (the function call stack will create a stack frame for each function call, not each function.)

Given the following code snippet, which of the following statement is NOT correct? int x; x = 3 + 4; A. 3 + 4 is an expression B. x = 3 + 4 is an expression C. 3 is not an operand D. the value of x before the assignment operation is random

C

Which of the following declaration is declaring a pointer that points to an integer that is not const-qualified? A. int const * p; B. const int * p; C. int * const p; D. const int * const p;

C

which of the following statement about goto statement is NOT true? A. label in goto statement has its own name space category B. label of goto statement is in function scope C. goto statement can be used to jump between different functions D. goto statement can be used to jump out of nested loops

C

which of the following statements about struct is NOT true? A. we can use dynamic memory allocation to control the size of the flexible array member in a struct B. simple assignment between struct objects of the same type is not performing a deep copy C. struct with flexible array member can be an element of array or a member of other struct D. simple assignment between struct objects of the same type functions as if we called memcpy()

C

For the following code snippet (with #number denoting the line number), which statement is true? #1 struct foo { #2 int n; #3 char c; #4 double arr[]; #5 } A. line 2, 3, 4: struct members are seperated by comma instead of semicolon B. this declaration of the struct is correc C. line 4 is not correct because arr is an imcomplete array D. line 5 is missing a semicolon after }

D

Name collision refers to the case when the following condition(s) is satisfied: A. entities belonging to the same name space B. entities are in the same scope C. entities sharing the same identifier D. all the other 3 conditions must be met at the same time

D

Which of the following statement about preprocessing directives is NOT correct? A. preprocessing of the macros are confined within the associated translation unit B. #define can be used to define a parameterized macro C. #include <stdio.h> will copy code written in stdio.h to the current source file D. #pragma once is the only way to set up an include guard for a header file

D

Which of the following statements about make and makefile is NOT correct? A. make is a ubiquitous build tool on UNIX system B. Makefile is like a recipe. It consists of rules for building object files and the executable program C. dependency list of a particular rule doesn't necessarily need to include the associated header files, but including header files is recommended since it will instruct make to keep track of changes made to these header files D. make followed by a rule name will instruct the make tool to run the associated command for the rule if such a rule is specified in the Makefile. This does NOT include the PHONY rules

D

n the following function definition, what type will the function parameter arr decay to? void foo(const double arr[const], int n) { /* function body */ } A. const double* B. double * const C. double const * D. double const * const

D

Which of the following statement about pointers is NOT true? A. a pointer is not the obect it is pointing to B. use dereference operator on a pointer will give you read and possiblly write access to the pointed object C. a pointer can store the address of another pointer D. use address of operator on a pointer will return the address stored in the pointer

D (apply address-of operator on a pointer will return the address of the pointer variable, not the address stored in the pointer.)

An array can be used to store a sequence of objects of different types True False

F

Both break and continue jump statements are used in (1) while statement (2) do-while statement (3) for statement and (4) switch statement, to transfer the control of a program. T/F?

F

Both scanf() and printf() are connected to stdin. TRUE FALSE

F

Both strcmp() and memcmp() return 1 if the input pointers point to memory blocks that store the same data. True False

F

For the following cope snippet: char dest[5]; char src[10] = "123456789"; strcpy(dest, src); will copy the first 5 characters in src to dest True False

F

If one member of a struct is const qualified, the whole struct is const qualified TRUE FALSE

F

If we want to convert a floating-point number in the string to a double typed value, we can use atof() (ASCII to float), for example: #include <string.h> int main(void) { char str[]="3.1415926"; double f = atof(str); return 0; } True False

F

Is the following example a correct way to set up the include guard for a header file named as bar.h? #ifdef BAR_H #define BAR_H /* actual code in the header file */ #endif TRUE FALSE

F

One can acquire the address of a register specified object by applying address-of operator on it TRUE FALSE

F

The operators of the same precedence do not always have the same associativity. T/F?

F

We can deallocate memory blocks for nodes of a binary tree by recursively freeing the associated nodes in preorder. TRUE FALSE

F

We can use strlen() to count the number of characters in a string, it returns number of characters stored in the string including '\0'. True False

F

constant expression used in switch statement can be a floating constant, e.g., 3.0, 2.5, etc. double flag = 3.0; switch(flag) { case 1.0: printf("case 1.0\n"); break; case 3.0: printf("case 3.0\n"); break; default: printf("default case\n"); } T/F?

F

if the src and dest pointers are pointing to memory blocks with overlapping, we should use memcpy(dest, src, size); instead of memmove(dest, src, size); to avoid undefined behavior. True False

F

in the following function, variable x has internal linkage void foo(int y) { static int x = 0; x += y; printf("x = %d\n", x); } TRUE FALSE

F

restrict qualifier is only applicable to pointer types including the function pointers TRUE FALSE

F

standard input stream, standard output stream, and standard error stream are all file streams declared in stdlib.h header file TRUE FALSE

F

the size of a struct object equals the sum of the sizes of all its members TRUE FALSE

F

volatile qualifier is the opposite of const qualifier TRUE FALSE

F

The size of an array can only be specified by a constant expression in C99 True False

F (C99 supports variable length array, see slides 10, page 4.)

A pointer to a non-const type can be implicitly converted to a pointer to const-qualified version of the same type, for example double * p = NULL; const double * cp = p; // type of p is (double *), implicitly converts to (const double *) TRUE FALSE

T

Any attempt to modify an object whose type is const-qualified results in undefined behavior TRUE FALSE

T

Both printf() and scanf() functions are variadic functions in C that can take a variable number of input arguments. T/F?

T

Building a program includes (1) compiling the source code to object code file(s) (2) and linking object code files and libraries together to form an executable program. T/F?

T

C is a lower-level programming language compared to Python or Java. T/F?

T

In the following function, variable x has automatic storage duration void foo(int x) { printf("x = %d\n", x); } TRUE FALSE

T

Inorder traversal with recursion visits a binary tree in the following order: 1) first finish the visit on the left subtree, 2) then visit the current root node 3) eventually finish the visit on the right subtree TRUE FALSE

T

String literal constructs an unnamed character array when a character string needs to be embedded in the source code True False

T

The C-style string is stored in a character array True False

T

We can declare a struct without its struct-declaration-list, and define the struct later with its struct-declaration-list. This is called forward declaration. (similar to function declaration vs function definition) True False

T

You can print a % character in the standard output using either a conversion character %% or an escape sequence \%, i.e., both of the print statements below will print one % character in your terminal: printf("%%"); printf("\%"); T/F?

T

a function declaration can be in file scope, function scope, or block scope. T/F?

T

a function prototype is a function declaration, but a function declaration may not be a function prototype. T/F?

T

char is an integral type in C, it has a limited range. T/F?

T

dereference a function pointer is the same as using the function pointer directly as if it's an alias for the pointed function. Similarly, apply & address-of operator to a function's identifier to fetch the address of the function is the same as using the function's identifier directly. For example: int foo(int, int); int (*fptr)(int, int) = foo; // is the same as int (*fptr)(int, int) = &foo; int ham = (*fptr)(3, 5); // is the same as int ham = fptr(3, 5); T/F?

T

file scope identifiers such as function names or file scope variables by default have external linkage TRUE FALSE

T

fopen() with "r" (read) mode will NOT give you write access to the file. Similarly, fopen() with "w" (write) mode will NOT give you read access to the file. TRUE FALSE

T

main function is the entry point of a C program T/F?

T

the conditional expression in a for statement is optional, if omitted, will be set to a non-zero constant, resulting in endless loops T/F?

T

the members of a union share the same memory address TRUE FALSE

T

the size of a union equals the size of its largest member TRUE FALSE

T

the tag name for a struct is optional in its declaration True False

T

use a pointer to an object as the input argument to a function is still "passing by value" from the pointer's perspective, but it achieves passing by reference from the pointed object's perspective. T/F?

T

we can use typedef to define an alias for a named struct: struct point {int x; int y;}; typedef struct point s_point; s_point a, b; // declare objects a and b to be of struct point type TRUE FALSE

T

we can use typedef to define an alias for a nameless struct: typedef struct {int x; int y;} s_point; s_point a, b; // declare objects a and b to be of struct {int x; int y} type TRUE FALSE

T

Building

The process composed of compiling and linking

Linking

The process of combining objectcode with libraries into an executable program

Terminal command to compile source code into an executable program

gcc filename.c -o myprogram

Compiling

the process of turning sourcecode into object code

For the following declaration of arrays, input their corresponding sizes (number of elements) int a[5]; int b[] = {1, 3, 5}; int c[] = {[10]=3}; int d[12] = {1, 2, 3); int e[11] = {}; the size of array a is the size of array b is the size of array c is the size of array d is the size of array e is

the size of array a is 5 the size of array b is 3 the size of array c is 11 the size of array d is 12 the size of array e is 11


Conjuntos de estudio relacionados

Chapter 26- Soft Tissue Injuries

View Set

Mid Terms Study Guide- Questions from the Book

View Set

Lab Report 11: Male and Female Reproductive Systems review

View Set

BASIC LIFE SUPPORT PRACTICE QUIZ

View Set

Unit 4.3.2: Developments in the global economy

View Set