Final Exam prep Flashcards
We can use the g++ compiler to read the high level code and store a translation in another file, the executable
True?
False?
True
Words in the C++ programming language are case insensitive
True?
False?
False
The ______ of a program language are its own precise grammar rules
a: syntax
b: semantics
c: execution
d: compilation
a: syntax
The ______ of a program represent the actual meaning of a segment of source code
a: indentation
b: semantics
c: syntax
d: code blocks
b: semantics
The assignment operator = allots the value of the right-side expression to the left operand
True?
False?
True
What is the final value of the variable b after the program is executed?
#include <cstdio></cstdio>
int main()
{
int a = 4, b = 0;
b = (a * 3) + 2;
printf(“%d”, b);
}
a: 18
b: 14
c: 0
d: 20
b: 14
Assume x = 2 and y = 3. What is the output of the following statement?
printf(“x=%d”, y);
a: y = 2
b: x = 2
c: x = 3
d: y = 3
c: x = 3
include <cstdio></cstdio>
What is the final value of the variable Pi after running the following program?
int main()
{
int age;
float myNumber;
int y = 3, z = 4;
float x = 0;
const float Pi = 3.1416;
int r = 7;
float area = 0;
Pi = Pi + 4
}
a: 0
b: None of the options listed. The program will not compile
c: 7.1416
d: 3.1416
b: None of the options listed. The program will not compile.
Note: the reason that the program will not compile is that 4 is not recognized by the program.
Note: declaration of a const means you cannot manipulate it
What is the output of the following program?
#include <cstdio></cstdio>
int main()
{
int a = , b = 3;
//b = a + 1;
//b = a * a;
b = b + a;
b = b * b;
//b = b + 2;
printf(“%d”, b);
}
a: 11
b: 0
c: 9
d: 3
Following up question: what is the purpose of %d?
b: 9
Answer for the following up question:
%d
prints the value of the int variable num in decimal number system
%o
prints the value of the int variable num in octal number system
#include <cstdio></cstdio>
int main()
{
//printf(“hello world!”);
//printf(“this is a second line”);
printf”this is the a third line”);
//printf(“Programming is fun!”);
}
a: hello world!
b: this is a third line
c: Programming is fun!
d: this is a second line
b: this is a third line
To use a funcion effectively, we only need to know:
_ its name
_ the type of data value (if any) it returns
True?
False?
False
return( printf(“%d”, ‘t’));
a: void
b: int
c: char
d: none of the options is correct
b: int
What would be the value assigned to x?
int x = increaseMe(increaseMe(increaseMe(3)))%7?
————————–
int increaseMe(int x)
{
int y = x + 1;
return(y);
}
a: 0
b: 1
c: 6
d: 7
c: 6
Functions are requied to have parameters.
True?
False?
False
What would be the value assigned to v?
int v = increaseMe(multiplyUs(6,5));
————————
int increaseMe(int x)
{
int y = x + 1;
return(y);
}
int multiplyUs(int n1, int n2)
{
return(n1/n2);
}
a: 31
b: 2
c: 1
d: 0
b: 2
printf(“%d”, ‘t’);
a: none of the options is correct
b: a floating point number
c: a number
d: char
c: a number
float multiplyUs(float n1, float n2)
{
float result = n1 / n2;
return(‘m’);
}
void printSomethin(int number)
{
cout «_space;increaseMe(number) «_space;endl;
}
True?
False?
False
printf returns a count of how many characters it displayed
True?
False?
True
When a return statement runs, the function immediately ends and sends back the return value
True?
False?
True
scanf returns a count of how many characters were assigned to a variable
True?
False
False
a: if(age > 10 || userDept == 7 && userDept == 13){}
b: if(age > 10 || (userDept == 7 && userDept == 13)){}
c:if(age > 10 && userDept == 7 && userDept == 13){}
d:if(age > 10 && (userDept == 7 || userDept == 13)){}
d:if(age > 10 && (userDept == 7 || userDept == 13)){}
int main()
{
int a = 1;
switch(a)
{
case 1:
cout«“One”;
case 2:
cout«“Two”;
case 3:
cout«“Three”;
default:
cout«“Default”;
}
}
a: Three
b: Default
c: One
d: None of the options listed
d: Two
d: None of the options listed
a flag is a Boolean variable signaling that some condition exists in the program. When the flag is set to false it indicates the condition does not exist. When the flag is set to true it indicates that the condition does exist.
True?
False?
True
int main()
{
if(5 == -1)
{
cout«“M”;
}
else
{
cout«“T”;
}
}
a: M
b: The code will not compile
c: None of the options listed
d: T
d: T
Which logical C expression correctly determines whether x and y are greater than z?
a: (x && y > z)
b: (x > z) && (y > z)
c: (x < z) & (y < z)
d: (x > y > z)
e: none of the options listed
remember:
a: logically wrong
correct answer: b
bool something(bool n)
{
if(n)
{
return(false);
}
return(something(true || n));
}
a: none of the options listed
b: true
c: false
d: the function definition is incorrect
c: false
A trailing else provides code that is executed when none of the conditions in the if/else if statements are true
True?
False?
True
The preferred parameter passing mechanism is called pass-by-value: It evaluates the value being passed, and copies that value to the function’s parameter
True?
False?
True
Global variables should generally be avoided: they complicate maintenance and debugging, since any function in the program could be manipulating them.
True?
False
True
Which of the following statements is false:
a: it is possible for multiple functions o have a local variable with the same name, but they’re all separate.
b: local variables can be accessed by other functions
c: local variables are visible within the function they are declared.
d: local variables are declared within a function
b: local variables can be accessed by other functions
Like variables, the contents of an array could be any values until/unless we specifically store something. The first time we store a value in an array position we are initializing that array element
True?
False?
True
The update statement is performed at the bottom of a for loop, just after each pass through the body
True?
False?
True
for (int i = 1; i <= 10; i = i + 2)
{
cout«i;
}
a: 13579
b: 1357
c: 1234568910
d: 1358
a: 13579
What loops need a semi colon after?
a: do
b: while
c: all of the options listed
d: for
a: do
What is the output of the following program:
int main()
{
int myArray[4] = {77, 33, 44, 8};
for(int i=0; i < 4; ++i) { cout << myArray[i]; } }
a: 7348
b: None of the options listed
c: 8443377
d: 7733448
d: 7733448
In nested loops, the outer loop runs to completion first (likely multiple times) before the inner loop is executed
True?
False?
False
For an array of size N, the positions are numbered 0…N.
True?
False?
False
reason: 0 to N-1
The following code will produce an infinite loop:
int main()
{
int i = 10;
while(i<100)
{
cout «_space;i «_space;endl;
}
}
True?
False?
True
What loops are guaranteed to execute at least once?
a: while
b: none of the options listed
c: d
d: for
c: do
An array is organized into positions, each acting as a storage spot for one item
True?
False?
True
When we define a struct we are actually defining a new data type: a structure made up of a variety of components, or fields, each given heir own name
True?
False?
True
When passing arrays as parameters to functions, the function can by default change the content of the array.
True?
False?
True
When functions include optional parameters, you must also consider the ambiguity of possible combinations of passed parameters.
T or F
True
In terms of order of parameter assignment, the parameters passed by the caller are assigned to the formal parameters in right-to-left order. Then any remaining parameters use their default values.
T or F
False
include <iostream></iostream>
What is the output of the following program:
using namespace std;
void swap(float &x, float &y);
int main()
{
const int Size = 5; float one[Size] = {1, 3, 5, 7, 9}; swap(one[0],one[Size-1]); swap(one[0],one[2]); for(int i=0; i < Size; ++i) { cout << one[i]; }
}
void swap(float &x, float &y)
{
float temp = x;
x = y;
y = temp;
}
a: 53971
b: 53179
c: 13579
d: 97531
e: None of the options listed
a: 53971
include <iostream></iostream>
What is the output of the following program:
using namespace std;
struct something
{
int x, y;
};
int main()
{
something a, b, c;
a.x = 1;
a.y = 3;
b.x = ++a.x;
c.y = 9;
cout «_space;c.y - b.x;
}
a: 9
b: 0
c: 1
d: none of the options listed
e: 7
e: 7
Binary search is more efficient than linear search
T or F
True
Structs allow us to group elements of a specific data type into a single logical entity, then refer to them by position.
T or F
False
include <iostream></iostream>
What is the output of the following program:
using namespace std;
int foo(int arr[], int size);
int main()
{
int goo[5] = {9,7,5,3,1}; cout << foo(goo,5);
}
int foo(int arr[], int size)
{
int total = 0;
for (int p=0; p<size; p=p+2)
{
total = total + arr[p];
}
return(total);
}
a: 25
b: None of the options listed
c: 0
d: 15
e: 9
d: 9
Binary search works on a sorted or unsorted array
T or F
False
When we define a struct we are actually defining a new data type: a structure made up of a variety of components, or fields, each given their own name.
T or F
True
Extra syntax can be added to the definition of a parameter to allow a function to alter its actual value. What is that syntax?
a: none of the options listed
b: &
c: %
d: =
b: &
The update statement is performed at the bottom of a for loop, just after
each pass through the body.
T or F
True
What is the output of the following program:
int main()
{
int myArray[4] = {77, 33, 44, 8};
for(int i=3; i >=0 4; --i) { cout << i; } }
a: 8443377
b: 0123
c: 3210
d: 7733448
a: 8443377
What value will be returned from evaluating something(4)?
int something(int n)
{
if(n==0)
{
return(0);
}
return(n * something(n-1));
}
a: 4
b: 24
c: 12
d: 0
d: 0
We can use & to look up the memory address of constants, variables, and parameters.
T or F
True
We can use a pointer to access the contents of an address in memory. This is is called dereferencing.
T or F
True
include <iostream></iostream>
What is the output of the following program:
using namespace std;
int main()
{
int *x, *y, X;
x = &X;
y = x;
*x = 7;
*y = 13;
cout «_space;X;
}
a: 7
b: 0
c: none of the options listed
d: 13
d: 13
We declare pointer variables by adding & to the data type
T or F
False
include <iostream></iostream>
What is the output of the following program:
using namespace std;
int main()
{
const int size =2;
int *p1, *p2;
int arr[size];
p1 = &arr[0]; p2 = arr; if( p1 == p2) { cout << 1; } cout << 7; }
a: 7
b: none of the options listed
c: 1
d: 17
d: 17