CPS188 Final Exam Study Questions AND Correct Answers
#include <stdio.h> - ✔✔header file library
adds functionality to c programs
a pointer is a variable that __ the __ __ of another variable as its value - ✔✔A
pointer is a variable that stores the memory address of another variable as its
value.
A pointer variable __ to a __ __ (like int) of the same type, and is created with
the * operator - ✔✔A pointer variable points to a data type (like int) of the
same type, and is created with the * operator.
equal to operator - ✔✔==
escape sequence \" - ✔✔inserts a double quote character
escape sequence \\ - ✔✔inserts a backslash character
escape sequence \t - ✔✔creates a horizontal tab
explain the components of scanf() - ✔✔scanf() takes two arguments: the format
specifier of the variable (%d in the example above) and the reference operator
(&myNum), which stores the memory address of the variable
Explain the relationship between pointers and arrays using this code, how
would you access the first element in the array?
,#include <stdio.h>
int main() {
int myNumbers[4] = {25, 50, 75, 100};
printf("%p\n", myNumbers);
printf("%p\n", &myNumbers[0]);
return 0;
} - ✔✔The memory address of the first element is the same as the name of the
array. Since myNumbers is a pointer to the first element in myNumbers, you can
use the * operator to access it
Explain the use of recursion in this code
#include <stdio.h>
int sum(int k);
int main() {
int result = sum(10);
printf("%d", result);
return 0;
}
int sum(int k) {
if (k > 0) {
return k + sum(k - 1);
, } else {
return 0;
}
} - ✔✔When you call sum with a number, let's say sum(10), the function checks
if the number k (10 in this case) is greater than 0.
If k is greater than 0, the function does two things: it keeps the current number
in the addition queue (return k + sum(k - 1);), and then it calls itself but with the
number k decreased by 1 (sum(k - 1)). This is the recursion part.
So, it's like saying, "Add 10, and then do everything again but starting with 9."
fill in the code
int day = 4; switch () {
case 1:
printf("Saturday");
break;
case 2:
printf("Sunday");
break;
// here
printf("Weekend");
} - ✔✔int day = 4; switch () {
case 1:
printf("Saturday");
break;
case 2:
#include <stdio.h> - ✔✔header file library
adds functionality to c programs
a pointer is a variable that __ the __ __ of another variable as its value - ✔✔A
pointer is a variable that stores the memory address of another variable as its
value.
A pointer variable __ to a __ __ (like int) of the same type, and is created with
the * operator - ✔✔A pointer variable points to a data type (like int) of the
same type, and is created with the * operator.
equal to operator - ✔✔==
escape sequence \" - ✔✔inserts a double quote character
escape sequence \\ - ✔✔inserts a backslash character
escape sequence \t - ✔✔creates a horizontal tab
explain the components of scanf() - ✔✔scanf() takes two arguments: the format
specifier of the variable (%d in the example above) and the reference operator
(&myNum), which stores the memory address of the variable
Explain the relationship between pointers and arrays using this code, how
would you access the first element in the array?
,#include <stdio.h>
int main() {
int myNumbers[4] = {25, 50, 75, 100};
printf("%p\n", myNumbers);
printf("%p\n", &myNumbers[0]);
return 0;
} - ✔✔The memory address of the first element is the same as the name of the
array. Since myNumbers is a pointer to the first element in myNumbers, you can
use the * operator to access it
Explain the use of recursion in this code
#include <stdio.h>
int sum(int k);
int main() {
int result = sum(10);
printf("%d", result);
return 0;
}
int sum(int k) {
if (k > 0) {
return k + sum(k - 1);
, } else {
return 0;
}
} - ✔✔When you call sum with a number, let's say sum(10), the function checks
if the number k (10 in this case) is greater than 0.
If k is greater than 0, the function does two things: it keeps the current number
in the addition queue (return k + sum(k - 1);), and then it calls itself but with the
number k decreased by 1 (sum(k - 1)). This is the recursion part.
So, it's like saying, "Add 10, and then do everything again but starting with 9."
fill in the code
int day = 4; switch () {
case 1:
printf("Saturday");
break;
case 2:
printf("Sunday");
break;
// here
printf("Weekend");
} - ✔✔int day = 4; switch () {
case 1:
printf("Saturday");
break;
case 2: