30
Quiz Question 1 – What is stored in A after the following have been executed double* A = 0x8000; A+=2; a. 0x8002 b. 0x8010 c. 0x8004 d. 0x8000

Quiz - Jee Whan Choi · Quiz •Question 1 –Which of the following methods is the correct way to declare a function pointer funcptrand assign the function double compute_average

  • Upload
    others

  • View
    1

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Quiz - Jee Whan Choi · Quiz •Question 1 –Which of the following methods is the correct way to declare a function pointer funcptrand assign the function double compute_average

Quiz

• Question 1 – What is stored in A after the following have been executeddouble* A = 0x8000;

A+=2;

a. 0x8002b. 0x8010c. 0x8004d. 0x8000

Page 2: Quiz - Jee Whan Choi · Quiz •Question 1 –Which of the following methods is the correct way to declare a function pointer funcptrand assign the function double compute_average

Quiz

• Question 2 – which if the following statements are correct?

a. When free() is called, specifying the number of allocated bytes is unnecessary because the compiler already knows how much memory had been allocated.

b. It is not necessary to check the return value for malloc() because the OS takes care of memory management

c. To allocate a 3-D array (e.g., int*** d3_array) two loops are requiredd. To store “Hello World” as a string, 11 bytes are required (remember

that space is also a character).

Page 3: Quiz - Jee Whan Choi · Quiz •Question 1 –Which of the following methods is the correct way to declare a function pointer funcptrand assign the function double compute_average

Quiz

• Question 3 – How many bytes are required to store an integer matrix of size (10 x 10 ), including the addresses (hint: count the number of bytes passed into ALL the malloc() functions)

a. 300 Bytesb. 408 Bytesc. 488 Bytesd. 888 Bytes

Page 4: Quiz - Jee Whan Choi · Quiz •Question 1 –Which of the following methods is the correct way to declare a function pointer funcptrand assign the function double compute_average

Quiz

• Question 4 – Which of the following is NOT the correct method for accessing the 6th element of the integer array A (i.e., int* A)?

• int* B = A; printf(“%d\n”, B[5]);• int* B = &(A[5]); printf(“%d\n”, B);• int* B = A + 5; printf(“%d\n”, *B);• int* B = &(A[0]); printf(“%d\n”, B[5]);

Page 5: Quiz - Jee Whan Choi · Quiz •Question 1 –Which of the following methods is the correct way to declare a function pointer funcptrand assign the function double compute_average

Quiz • Question 1 – Which of the following methods is the correct way to declare a function pointer funcptr and assign the function double compute_average(double *y, int cnt) to it

a. double (funcptr) (double* y, int cnt); funcptr = &compute_average;

b. double (*funcptr) (double* x, int cnt); funcptr = &compute_average;

c. double (funcptr) (y, cnt); functpr = compute_average;

d. double (*funcptr) (double* y, int cnt); *funcptr = compute_average;

Page 6: Quiz - Jee Whan Choi · Quiz •Question 1 –Which of the following methods is the correct way to declare a function pointer funcptrand assign the function double compute_average

Quiz

• Question 2 – Which of the following is NOT correct?

a. We can pass a function pointer as a parameter to another functionb. A 2-D matrix stores all of its data consecutively in memory (just like a 1-D

matrix)c. In some cases, we can use a function that takes in a 2-D matrix as input

(e.g., int** some_array) for processing a 1-D matrix (e.g., int* some_other_array)d. We can create a 100 arrays using a single malloc()

Page 7: Quiz - Jee Whan Choi · Quiz •Question 1 –Which of the following methods is the correct way to declare a function pointer funcptrand assign the function double compute_average

Quiz

• Question 3 – What will be the output when the following code is executed?

a. 101b. 102c. some addressd. undefined behavior

int some_func(int* i) {

int j = *i;return j++;

}

int i = 101;some_func(&i);fprintf(stdout, "%d\n", i);

Page 8: Quiz - Jee Whan Choi · Quiz •Question 1 –Which of the following methods is the correct way to declare a function pointer funcptrand assign the function double compute_average

Quiz • Question 4 – Which of the following statements is correct?

a. b and b_tmp have different data types, so it will not return a valid address to the main function (where my_malloc() was called)

b. Since malloc’d memory was assigned to a variable created inside of a function (i.e., b_tmp is on the stack), it will be lost when the function returns

c. It behaves like a smart malloc() function, where the return value from malloc() is checked to see if it’s valid

d. None of the above are true.

void my_malloc(double** b){

double* b_tmp;b_tmp = (double*) malloc(sizeof(double) * 128);assert(b_tmp);fprintf(stdout, "My address is %p\n", b_tmp);*b = b_tmp;

}

Page 9: Quiz - Jee Whan Choi · Quiz •Question 1 –Which of the following methods is the correct way to declare a function pointer funcptrand assign the function double compute_average

• Which of the following statement is NOT true1. Keeping your program in multiple files makes it easier to compile your code2. You can find memory leak bugs using only printf statements3. You need #ifndef in header files to prevent multiple definitions/inclusions of

functions, header files, definitions, etc.4. gdb is good for finding where seg faults occurs and valgrind is good for

finding where memory leak occurs

Page 10: Quiz - Jee Whan Choi · Quiz •Question 1 –Which of the following methods is the correct way to declare a function pointer funcptrand assign the function double compute_average

• gcc is a compiler driver. It uses several tools to convert high-level code (e.g., C, C++) to executable code. Which of the following is NOT one of those tools?

1. Macro expander2. Linker3. Assembler4. Preprocessor

Page 11: Quiz - Jee Whan Choi · Quiz •Question 1 –Which of the following methods is the correct way to declare a function pointer funcptrand assign the function double compute_average

• In the ELF layout, where is the program code kept?1. .data section2. .symtab section3. .text section4. Program headers/segments section

Page 12: Quiz - Jee Whan Choi · Quiz •Question 1 –Which of the following methods is the correct way to declare a function pointer funcptrand assign the function double compute_average

• Which of the following are NOT true about static and dynamic libraries

1. Group of answer choicesStatic libraries are created using ar and dynamic libraries are created using gcc with the --shared flag

2. Dynamic libraries typically results in larger executable file size, due to every function in the library being included in the executable

3. Static libraries concatenates object files into a single file with an index4. When static libraries are used, every function required by the calling

program is included in the program’s executable

Page 13: Quiz - Jee Whan Choi · Quiz •Question 1 –Which of the following methods is the correct way to declare a function pointer funcptrand assign the function double compute_average

Quiz

Question 1 - Which of the following statements is NOT true

A. Each class has three boundaries - public, private, and protected

B. Class can be seen as a data type, where as an object as a variable

C. If an item in a class is private, it cannot be accessed by an inherited class in any way, even if they use the provided interface - they have to be protected

D. The Is-a idea suggests that derived class should be EXACTLY like the base class

Page 14: Quiz - Jee Whan Choi · Quiz •Question 1 –Which of the following methods is the correct way to declare a function pointer funcptrand assign the function double compute_average

class King : public Human {public:

void get_str() { cout << "King has

strength " << str << “ “; }

};

This Question is worth 2 points.Question 2 - What would be printed by the following code:

Quiz

class Human {public:

void get_str() { cout << "Human has

strength " << str << “ “; }

void set_str(unsigned int x) {

str = x; }

protected:unsigned int str;

};

void some_func(Human& h) { h.get_str(); }

Human a; King b;a.set_str(100); b.set_str(200);some_func(a); some_func(b);

A. Human has strength 100 Human has strength 200B. Human has strength 100 King has strength 200C. King has strength 100 King has strength 200D. King has strength 100 Human has strength 200

Page 15: Quiz - Jee Whan Choi · Quiz •Question 1 –Which of the following methods is the correct way to declare a function pointer funcptrand assign the function double compute_average

Question 3 - Which of the following statements is true?

A. string class and vector class have dynamic sizes, whereas C string and arrays have fixed sizes

B. string class CANNOT be converted to C strings

C. malloc and new can be mixed without any problems when allocating memory

D. struct cannot contains functions in C++ (i.e., just like C)

Quiz

Page 16: Quiz - Jee Whan Choi · Quiz •Question 1 –Which of the following methods is the correct way to declare a function pointer funcptrand assign the function double compute_average

Question 1 - Which of the following statements are NOT true

A. Members are public by default in structsand prviate by default in classes

B. You can initialize private members ONLY by using constructors

C. Constructors cannot be called explicitlyD. Constructors can be overloaded

Quiz

Page 17: Quiz - Jee Whan Choi · Quiz •Question 1 –Which of the following methods is the correct way to declare a function pointer funcptrand assign the function double compute_average

Quiz

Question 2 - For the following code, what is the output?

A. 0B. No output - it will generate a compiler errorC. No output - it will generate a runtime errorD. 100

1. struct Rectangle {

2. private:

3. float width;

4. float height;

5. public:

6. Rectangle(int w, int h) { width = w; height = h; }

7. float area() { return width * height; }

8. };

9. Rectangle A;

10. cout << A.area() << endl;

Page 18: Quiz - Jee Whan Choi · Quiz •Question 1 –Which of the following methods is the correct way to declare a function pointer funcptrand assign the function double compute_average

Question 3 - What will the following piece of code do?

A. Create a Square object and set width and height to 1.0B. Create a Square object and set width and height to 0.0C. Create a Rectangle object and set width and height to 1.0D. None of the above

1. struct Rectangle {

2. private:

3. float width;

4. float height;

5. protected:

6. float some_random_value;

7. public:

8. Rectangle() { width = 0.0; height = 0.0; }

9. Rectangle(int w, int h) { width = w; height = h; }

10. float area() { return width * height; }

11. };

12. struct Square : public Rectangle {

13. Square() { width = 1.0; height = 1.0; }

14. };

main.c

1. Square B;

Quiz

Page 19: Quiz - Jee Whan Choi · Quiz •Question 1 –Which of the following methods is the correct way to declare a function pointer funcptrand assign the function double compute_average

Question 1Which of the following are true about the this variable?

A. It is a copy to the objectB. When the object is allocated at the beginning of its

scope, this points to initialized memoryC. It can be used to differentiate between parameter

variables and member variablesD. It is passed to only static member functions

Quiz

Page 20: Quiz - Jee Whan Choi · Quiz •Question 1 –Which of the following methods is the correct way to declare a function pointer funcptrand assign the function double compute_average

Quiz

class Test

{

private:

int x; int y;

public:

Test(int x = 0, int y = 0) { this->x = x; this->y = y; }

Test &setX(int a) { x = a; return this; }

Test &setY(int b) { y = b; return this; }

void print() { cout << "x = " << x << " y = " << y << endl; }

};

int main()

{

Test obj1(5, 5);

// Chained function calls. All calls modify the same object

// as the same object is returned by reference

obj1.setX(10).setY(20);

obj1.print();

return 0;

}

Question 2Given the following code, which of the following options are true?

A. It will not compileB. It will print x = 10 y

= 20C. It will print x = 5 y =

20D. It will print x = 5 y =

5

Page 21: Quiz - Jee Whan Choi · Quiz •Question 1 –Which of the following methods is the correct way to declare a function pointer funcptrand assign the function double compute_average

D.Test(int)TestMoreTest(int j)x = 0 y = 0~Test~Test~MoreTest

C.Test(int)TestMoreTest(int j)x = 5 y = 5~MoreTest~Test~Test

B.TestTest(int)MoreTest(int j)x = 0 y = 0~Test~Test~MoreTest

class Test{private:

int x; int y;public:

Test() { x = 0; y = 0;cout << "Test" << endl;

}Test(int i) {

x = i; y = i;cout << "Test(int)" << endl;

}~Test() { cout << "~Test" << endl; }void print() { cout << "x = " << x <<

" y = " << y << endl; }};

Quiz

Question 3Given the following piece of code, what will be the output?

class MoreTest : public Test{public:

MoreTest() { cout << "MoreTest" << endl; }MoreTest(int j) : Test(j) { cout <<

"MoreTest(int j)" << endl; }~MoreTest() { cout << "~MoreTest" << endl; }

};

int main(){

Test x;MoreTest y(5);y.print();return 0;

}

A.TestTest(int)MoreTest(int j)x = 5 y = 5~MoreTest~Test~Test

Page 22: Quiz - Jee Whan Choi · Quiz •Question 1 –Which of the following methods is the correct way to declare a function pointer funcptrand assign the function double compute_average

Quiz

Question 4Which of the following statements are true?

A. Caesar and ROT13 ciphers cannot be implemented using a substitution cipher

B. ROT13 is a specific/special case of Caesar cipher

C. Vigenere cipher is unbreakableD. Running key cipher is unbreakable for any

cipher key

Page 23: Quiz - Jee Whan Choi · Quiz •Question 1 –Which of the following methods is the correct way to declare a function pointer funcptrand assign the function double compute_average

Question 1 - what is the output of the following piece of code?

int a[] = {1, 2, 3, 4, 5};int *b = (int*) malloc(sizeof(int) * 128);printf("%lu\n", sizeof(a));printf("%lu\n", sizeof(b));

A. 168

B. 208

C. 1616

D. 88

Quiz

Page 24: Quiz - Jee Whan Choi · Quiz •Question 1 –Which of the following methods is the correct way to declare a function pointer funcptrand assign the function double compute_average

Quiz

Question 2 - What would happen with the following piece of code?struct Y {

float f;

int i;

Y(int a);

};

Y y2[2] = { Y(1) };

A. No problems - code will be compiled and executed without any issues

B. Code will be compiled but there will be a runtime error because 1 will be passed in as a double by default

C. Compiler will create a default constructor (with 0 for the argument) and use that to initialize Y2[1];

D. Compiler will give you an error because there is no constructor for Y2[1];

Page 25: Quiz - Jee Whan Choi · Quiz •Question 1 –Which of the following methods is the correct way to declare a function pointer funcptrand assign the function double compute_average

Quiz

Question 3 - which of the following statements are correct for the following code?int main() {

union {

int i;

float f;

};

i = 12;

f = 1.22;

}

A. Name for the union is missingB. Type for the union is missingC. There is nothing wrong - this is a nameless unionD. There is nothing wrong - this is an anonymous union

Page 26: Quiz - Jee Whan Choi · Quiz •Question 1 –Which of the following methods is the correct way to declare a function pointer funcptrand assign the function double compute_average

Quiz

Question 4 - what is the output for the following piece of code?void f(int i, int j);int main(){

f(10);f(10, 1000);

}

void f(int i, int j = 100){

cout << "i is " << i << “ “;cout << "j is " << j << endl;

}A. i is 10 j is 100

i is 10 j is 1000A. i is 10 j is 100

i is 10 j is 100A. i is 10 j is 1000

i is 10 j is 1000

A. None of the above

Page 27: Quiz - Jee Whan Choi · Quiz •Question 1 –Which of the following methods is the correct way to declare a function pointer funcptrand assign the function double compute_average

Quiz

Question 1 - Which of the following are NOT true?A. const can be used to replace #defineB. const allows type checking whereas #define does not

C. constant folding is where you reuse an identical calculation that has already been done before to skip calculation at runtime

D. const are allowed in header files

Page 28: Quiz - Jee Whan Choi · Quiz •Question 1 –Which of the following methods is the correct way to declare a function pointer funcptrand assign the function double compute_average

Quiz

#include <iostream>extern int globe;int func(){

globe = 10;return globe;

}

#include <iostream>using namespace std;

static int globe;int func();

int main(){

globe = 100;func();cout << globe << endl;return 0;

}

what will be printed?

A. 10B. 100C. Compile errorD. Undefined behavior

Page 29: Quiz - Jee Whan Choi · Quiz •Question 1 –Which of the following methods is the correct way to declare a function pointer funcptrand assign the function double compute_average

Question 3 - Given the following piece of code which of the following options list the lines in the code that will cause a compile error?

#include <iostream>using namespace std;

1. const int i[] = {1, 2, 3, 4};2. float f[i[0]];

1. int main(int argc, char** argv)2. {3. const int j[] = {1, 2, 3, 4};4. float g[i[0]];5. float h[j[0]];6. }

A. Only lines 2, 6, and 7B. Only lines 2 and 6C. Only line 2D. Only line 6E. Only line 7

Quiz

Page 30: Quiz - Jee Whan Choi · Quiz •Question 1 –Which of the following methods is the correct way to declare a function pointer funcptrand assign the function double compute_average

Quiz

Question 4 - Given the following piece of code which of the following options list the lines in the code that will cause compile error?

1. int d = 1;2. const int e = 2;3. int* const f = &e;4. int* u = &d;5. int* v = &e;6. int* w = (int*)&e;7. int main() {}

A. Only lines 3, 4, 5, and 6B. Only lines 4 and 5C. Only line 5D. Only lines 5 and 6E. Only lines 3 and 5