CSE 240 Midterm Material
Given the following code and considering things like ASCII values and pointer addresses: int num1 = 5; //addressed at 1767612 int num2 = 10; //addressed at 1767600 int num3 = 15; //addressed at 1767588 char ch1 = 'a'; //addressed at 3734375 char ch2 = 'b'; //addressed at 3734363 char ch3 = 'c'; //addressed at 3734351 char* chPtr = &ch3; int* iPtr = &num3; *iPtr = num3 * 8; *chPtr = *iPtr; What will the following statement output? cout << ch3;
'x'
C/C++ has 2 pointer operators, which operator represents the name of the address? , *, &, :
*
Given the following snippet of code, answer the following two questions based on the code: typedef enum {red, amber, green} traffic_light; traffic_light x = red, y = green; while (x != y) { x++; } y++; printf("x = %d, y = %d", x, y); 1. What value will be printed for variable x? Please choose: 2 . 2. What value will be printed for variable y? Please
1. 2 2. 3
Given this snippet of code, what is the value of x after executing the last statement? int x = 10, *y; y = &x; *y = 100;
100
Given this snippet of code, what is the value of x after executing the last statement? int x = 10, *y, **z; y = &x; z = &y; **z = 1000;
1000
Given the following: int num1 = 5; //addressed at 1877112 int num2 = 15; //addressed at 1877300 int num3 = 20; //addressed at 1877192 double d1 = 1.05; //addressed at 1374376 double d2 = 2.25; //addressed at 1374360 double d3 = 3.14; //addressed at 1374344 After these statements: double* ptr1 = &d2; ptr1 = ptr1 - 2; What will be the address contained in ptr1?
1374344 ... to get this, 1374360 -2 *16. We get the value 16 because the bytes for a double is 8 and we're subtracting two double so it would be 16
What's the size of this struct? struct record4 { int a; int c; float e; char b; char d; };
16
What's the size of this struct? struct record3 { int a; char b; int c ; char d; float e; };
20
What is the size of this struct? struct record1 { double x; char p[10]; char c; int a; };
24
What is the size of this struct? struct record1 { char p[10]; double x; char c; int a; };
32
Given: char: 1 short: 2 int: 4 long: 4 float: 4 double: 8 How many bytes is this array: char* myStrings[10]
40
Assume this is a 32-bit environment, given a declaration: int a[10]; What is the size (in bytes) of the entire array?
40 since int is 4 bytes and we have 10 so 4*10 = 40
Given this macro and this code snippet: #define SLOPE(xa, ya, xb, yb) yb-ya / xb - xa int x1 = 1; int y1 = 1; int x2 = 5; int y2 = 7; float slope = SLOPE(x1,y1,x2,y2); cout << slope << endl; What value will slope have? Do yourself a favor and calculate it by hand. Remember order of operations and the nature of division on C/C++/Java. If you do just dump it in a compiler - make sure you understand why the result is the way it is. Also realize different compilers MAY give you different results. Order of operations on VS is different than gcc/g++
6
Definition of Number Orthogonality
A general lack of restriction on reproducing features in your code
Implicit type conversion is commonly referred to as
Coercion
Dynamically allocated data/objects/etc. are created and exist on the Stack part of RAM T or F?
False
Given this macro and this code snippet: #define SLOPE(xa, ya, xb, yb) yb-ya / xb - xa int x1 = 1; int y1 = 1; int x2 = 5; int y2 = 7; float slope = SLOPE(x1,y1,x2,y2); cout << slope << endl; Will this code will give me the correct slope calculation of slope = 1.5? T or F?
False
Given: Very Simple Programming Language (VSPL) <char> ::= a | b | c | ... | z | 0 | 1 | ... | 9 <operator> ::= + | - | * | / | % | < | > | == | >= | <= <variable> ::= <char> | <char> <variable> <expr> ::= <variable> <operator> <variable> | ( <expr> ) <operator> ( <expr> ) <assign> ::= <variable> = <expr>; <statements> ::= <assign> | <assign> <statements> The following is valid: a = b + c + d; T or F?
False
Given: Very Simple Programming Language (VSPL) <char> ::= a | b | c | ... | z | 0 | 1 | ... | 9 <operator> ::= + | - | * | / | % | < | > | == | >= | <= <variable> ::= <char> | <char> <variable> <expr> ::= <variable> <operator> <variable> | ( <expr> ) <operator> ( <expr> ) <assign> ::= <variable> = <expr>; <statements> ::= <assign> | <assign> <statements> The following is valid: T or F? (a + b) = (x * y);
False
In Backus Naur Form, non-terminals contained in () are required in the syntax. T or F?
False
Reliability speaks to how easily a programmer can express themselves reliably within the language. T or F?
False
When creating a multi-dimensional array using compile time( on stack RAM) it is possible to create a ragged array, which is a multi-row array where the rows are not the same length. T or F?
False
With over 500 programming languages in the world, the best approach to learning languages is to focus on memorizing syntax and structure. Then learn languages with similar syntaxes and structures. T or F?
False
Given the following code char *p = "hello", *s = "this is a string"; p = s; printf("%s\n", p); What will happen?
It prints: this is a string
What computing paradigm can solve a problem by describing the requirements, without writing code in a step-wise fashion to solve the problem?
Logic
Definition of Compositional orthogonality
Many-to-Many
Can the identifier "base_variable" be created from the following BNF rulset? <char> :: = a | b | c| ... | s |... | z <identifier> ::= <char> | <char> <identifier>
No, there is an underscore in the identifier name that cannot be generated
Which programming paradigm is focused primarily on the data and how it is modeled and used?
Object-Oriented
What is the main reason of applying two-step translation of high level programming language?
One compiler for all machines
Definition of Sort orthogonality
One-to-Many
The user or programmer is being exceptionally stupid.
PEBCAK error
When the function does not want to modify the value, the value is expensive to copy, and NULl is valid
Pass by Constant Pointer
When the function wants to modify the value, the value is expensive to copy, and NULL is valid
Pass by Pointer
When the function does not want to modify the parameter and the value is easy to copy
Pass by Value
What are the two differences between a global variable and a static variable?
Scope and Static Keyword
Is the program doing the right things?
Semantic error
If I have a set of commands S1 that describe data types in my language and a set of commands S2 that describe creation of variables: S1: int, decimal, bool S2: <type><name>,<type><name> = <value>,constant<type><name> = <value> The set S3: int <name>, int <name> = <value>, constant int <name> = <value> -- demonstrates what kind of orthogonality?
Sort Orthogonality
Which part of RAM are automatically controlled by the program/memory manager
Static and Stack
Is the statement built correctly?
Syntax error
Backus Naur Form allows for recursive definitions of non-terminal symbols. The definitions are usually separated with a | (aka pipe) T or F?
T
A data type defines what?
The values and operations allowed
All pointers are the same size and that size is dependent on the architecture of the processor. T or F?
True
Autocode and FORTRAN are considered to be the first high-level programming languages. T or F?
True
C/C++ is perfectly happy to allow the programmer to walk outside the bounds of an array. An error can/will eventually occur when you interact with memory that the Memory manager has reserved. T or F?
True
Classes in C++ follow the same general syntax pattern as Structs, Enums, etc. <typename><name> { <body> }; T or F?
True
Given: Very Simple Programming Language (VSPL) <char> ::= a | b | c | ... | z | 0 | 1 | ... | 9 <operator> ::= + | - | * | / | % | < | > | == | >= | <= <variable> ::= <char> | <char> <variable> <expr> ::= <variable> <operator> <variable> | ( <expr> ) <operator> ( <expr> ) <assign> ::= <variable> = <expr>; <statements> ::= <assign> | <assign> <statements> The following is valid: T or F? a = x + y; b = s * t; c = w + v;
True
Learning the concepts, logic, and problem solving of programming unlocks our ability to learn new languages. T or F?
True
Multiple pointers can reference the same objects. T or F?
True
Assume a varible is declared in a block of code within a pair of curly braces. The scope of the variable ______ a) starts from its declaration point and extends to the end of the block. b) starts from its declaration point and extends to the end of the entire program. c) covers the entire block, including the code before the declaration point. d) covers the entire program, including the code in the other blocks.
a
What is a good rule of thumb for sizing a Struct in C/C++ assuming that the Struct is optimally set up? a) Count all the bytes of the data in the struct. Find the nearest power of 2, 4 or 8 based on the largest Data Type in your struct. If the total number of bytes is larger than that power, then the struct will be the next power in size. b) Count all the bytes of the data-types in the struct. Find the nearest power of 2, 4 or 8 based on the largest member in your struct. If the total number of bytes is larger than that power, then the struct will be the next power in size. c) Count all the bytes of the data in the struct. Find the nearest power of 2 based on the largest array in your struct. If there isn't an array use the largest variable. If the total number of bytes is larger than that power, then the struct will be the next power in size. d) The largest data-type in your struct raised to the power of how many members are in the struct.
a
When evaluating a programming language the category efficiency describes: a) The 'first concern' historically. Concerned with how fast the code compiles and runs b) This concept is concerned with the simplicity and understandability of the language... features, constructs, orthogonality, etc. c) This concept is concerned with the actual production of code and the ability of the programmer to easily use the language... syntax, abstraction, expressivity... d) This concept is concerned with the internal safety and consistency of the language... type checking, exceptions, etc... e) This concept asks how tied down a language is to a particular platform, can code be distributed easily and can libraries be made and shared
a
Which of the following types is a C++ type, but NOT a C type? a) bool b) char c) double d) void
a) bool
What does the | (pipe) symbol in a BNF rule mean? a) it is an or statement. Choose one of the options. b) It is a terminal symbol and will be in the final string c) it indicates a comment in a BNF rule d) the concatenation of two adjacent terminal symbols
a) it is an or statement. Choose one of the options.
During compilation, linker is used for a) resolving external references (bring in code from other libraries) b) resolving conflicts within your program c) translating a high level language program to assembly code/machine code d) translating assembly program to binary code
a) resolving external references
In contrast to Web 1.0, what is the key function of Web 2.0? a) web is the computing platform b) web supports graphic display c) web supports semantic analysis d) web is accessed over HTTP protocol
a) Web is the computing platform
What is true about encapsulation in c++ vs. java? (check all) a) there is no real difference in concept, just in syntax. b) in C++ private and protected are the same thing c) java technically has a 4th visibility modifier. When you don't put a visibility modifier on a property or method it gets the default modifier which makes it partially private d) c++ only allows you to use public, private, and protected once each
a, c
What programming language characteristics impact the readability of the programs written in this language? Select all. a) control structures b) type checking c) syntax design d) data structures
a, c, d
Why does the datatype of the pointer matter? Select all that apply a)The datatypes have rules about addressing the pointer needs to work within those rules b)It's only a readability feature that makes it clear what a pointer is for c)If I dereference a pointer, the datatype informs how many bytes are going to be interpreted and how to interpret them d)When we do pointer arithmetic, the number of bytes the address changes is based on data type
a, c, d
The imperative programming paradigm is popular and is a part of object-oriented computing paradigm, because it ____ (Select all answers that apply). a) is based on computer organization and thus is efficient to execute on hardware. b) matches the culture of doing things by following the step-wise instructions. c) enforces the modularity and supports the development of large programs. d) supports class inheritance and code reuse.
a,b
When evaluating a programming language the category readability describes: a) The 'first concern' historically. Concerned with how fast the code compiles and runs b) This concept is concerned with the simplicity and understandability of the language... features, constructs, orthogonality, etc. c) This concept is concerned with the actual production of code and the ability of the programmer to easily use the language... syntax, abstraction, expressivity... d) This concept is concerned with the internal safety and consistency of the language... type checking, exceptions, etc... e) This concept asks how tied down a language is to a particular platform, can code be distributed easily and can libraries be made and shared
b
Event-driven computing paradigm is to: a) enable multiple tasks running on different processors b) define a set of events and write an event handler for each event c) require the program to execute in a predefined order d) write requirements without having to write any program
b) define a set of events and write an event handler for each event
Given this snipped of code in Java: char alpha = 'a'; int numeric = alpha + 10; which of the following statements is correct? a) syntactically incorrect, but contextually correct b) syntactically correct, but contextually incorrect c) syntactically correct and contextually correct d) syntactically incorrect and contextually incorrect
b) syntactically correct, but contextually incorrect
Java uses which of the following program processing? a) two step translation without intermediate code b) two step translation with intermediate code c) interpretation d) compilation
b) two step translation with intermediate code
Programming languages can be broken down into four structural layers. Identify these layers. a) dictionary b) semantics c) contextual d) syntactic
b, c, d
Given the declaration: char a[] = "Hello";char *p = a, *q; what operations are valid syntactically? Select all that apply. a) q = &&a[0]; b) q = &(*p); c) q = &&a; d) *q = *(&a[0]);
b, d
Select all that apply: Which statements indicate why it is important to understand dynamic multi-dimensional arrays as "arrays of arrays"? a) It helps us figure out how to get the computer to tell us the size of each dimension of the array and the size of the array itself. b) Dynamic allocation requires multiple pointers and each pointer can 'point' to it's own array c)The memory is constructed such that it could actually be understood as a single dimension array if you're willing to do the math. d) Each row is independently created at run-time e) If we get that each row is an independent array, it makes the idea of passing single rows or other subsections of the multi-dimensional array to functions easier. f) This is the only way to create "ragged or jagged" arrays in C/C++. It might be wise to keep a parallel array of each row's size.
b,d,e,f
Given the following code char a[2][3] = { { 'c', 'a', 't'}, { 'd', 'o', 'g'} };int i, j;for (i = 0; i<2 ; i++) {for (j = 0; j<3; j++)printf("%c", a[i][j]);} What will happen? a) It prints: cat b) A compilation error will occur at this line: char a[2][3] = { { 'c', 'a', 't'}, { 'd', 'o', 'g'} }; c) It prints: catdog d) A compilation error will occur at this line: printf("%c", a[i][j]);
c
In C++, what function can be used to input a string with spaces? a) scanf("%s", &str); b) cin >> str; c) cin.getline(...); d) scanf.getline(...);
c
Which of the following C assignment statements (assign a value to a variable at the semantic level) will NOT cause a compilation error? Assume the array has been declared as: char a[5]; a) a[] = {'h', 'i'}; b) a = "hi"; c) a[0] = 'h'; d) a = "h";
c
Given the declaration: char a[] = "Hello";char *p = a, *q; what operations are valid syntactically? Select all that apply. a) q = &&a[0]; b) q = &&a; c) q = &(*p); d) *q = *(&a[0]);
c, d
Given the following definition and declarations: #define size1 10 const int size2 = 20; char a1[size1]; char a2[size2]; which line of code can cause a compilation error?
char a2[size2];
Is the statement built from the right stuff?
context error
When evaluating a programming language the category reliability describes: a) The 'first concern' historically. Concerned with how fast the code compiles and runs b) This concept is concerned with the simplicity and understandability of the language... features, constructs, orthogonality, etc. c) This concept is concerned with the actual production of code and the ability of the programmer to easily use the language... syntax, abstraction, expressivity... d) This concept is concerned with the internal safety and consistency of the language... type checking, exceptions, etc... e) This concept asks how tied down a language is to a particular platform, can code be distributed easily and can libraries be made and shared
d
When is padding required for a structure type variable? a) When the structure contains character type of variables. b) As long as the size of the structure is not a multiple of four. c) As long as the structure contains a word-type variable, such as integer, float, and pointer. d) When the structure contains a word-type variable, such as integer, float, and pointer, and the total number of bytes is not a multiple of four.
d
Which of the following coding practice can lead to a higher memory efficiency (use fewer padding bytes) in defining a structure? a) The order of the members in a strucuture does not increase or decrease the structure size. b) Keep the word-type variables together. c) Place the word-type of variables (such as int, float, pointer) between the character-type variables. d) Keep the character type variables together.
d
Given the declaration: char A[3] = {'C', 'a', 'r'}, *p = A; what statement prints character a? a) printf("%c", *p++); b) printf("%c", *p+1); c) printf("%c", *p); d) printf("%c", *(++p));
d)
What is the major improvement of structured programming languages over the earlier programming languages? a) introducing variables b) allowing code reuse c) not allowing the use of variables d) removing Goto statement from the language
d) Removing Goto statement from the language
Which of the following statements will assign the address of the variable int myInt to the pointer int* myPtr? a) int* myPtr = myInt; b) int* myPtr = *myInt; c) int& myPtr = &myInt d) int* myPtr = &myInt;
d) int* myPtr = &myInt;
If I dynamically allocate a 1D array in C++, what command do I use to release the memory?
delete[]
When evaluating a programming language the category Reusability describes: a) The 'first concern' historically. Concerned with how fast the code compiles and runs b) This concept is concerned with the simplicity and understandability of the language... features, constructs, orthogonality, etc. c) This concept is concerned with the actual production of code and the ability of the programmer to easily use the language... syntax, abstraction, expressivity... d) This concept is concerned with the internal safety and consistency of the language... type checking, exceptions, etc... e) This concept asks how tied down a language is to a particular platform, can code be distributed easily and can libraries be made and shared
e
What is a feature of object-oriented computing?
encapsulation of states
What computing paradigm enforces stateless ( no variable allowed ) programming?
functional
Given this snippet of code, what is the value of z after executing the last statement? int x = 10, *y, **z; z = &y; y = &x; *y = 100;
idrk
What is the maximum number of padding bytes that a compiler can add to a structure?
more than 4
Where is the main() function located in a C or C++ program?
outside any class
What data types in C have the same data range assuming that the computer used is a 32 bit computer?
pointer type and unsigned int