1/71
Looks like no tags are added yet.
What does this code print out?
double x = 12.482168;
printf( "%.1lf", x );
12.5
What is the format specifier for printf when printing a double?
%lf
Where is the entry point of a C program?
main() function
What does the following code print out?
int x;
printf( "%d", x );
garbage
In C, what is the size in bytes of a char?
1
What function is used to get user input from the standard input (usually the keyboard) ?
scanf
What is the escape character for a tab?
\t
In order to use math functions, which header file must be included?
math.h
What's the value of the expression: 5 / 2 * 2.0 ?
4.0
T/F: All C variables must be declared at the top of the block (function declaration, etc)
false
T/F: Declaration, allocation, and initialization of a variable are always done together. You cannot do one but not the other(s).
F
What causes overflow in some integer expressions ?
Because the result exceeds the maximum (or minimum) value representable by the data type.
Which of the following is not true about Data Types?
The compiler doesn't care about type declarations. It's only used by human programmers.
When does the C compiler add implicit type conversion between integers and floats?
When an integer and a float appear in the same expression.
consider the following statement:
if (X) if (Y) S1 else S2 else S3Which of the following sentences is correct?
S2 will be executed when X is true and Y is false
What gets printed ?
int a = 0;
if (a = 5) printf( "Alpha" ); else printf( "Beta" );
Alpha
consider the following statement:
if (A) {
if (!B) S1 else S2
} else S3
Which of the following sentences is correct?
S3 will be executed when A is false even if B was false.
Given the following declaration:
int x = 0.5 ; What do you think will happen when trying to compile/run this statement:
if (x != 0 && 10 / x > 5) printf("Hello!") ; The statement will compile and run but nothing will be printed.
How many times does this while loop iterate?
int a = 2 ;
while ( a >= 0 ) {
a = a - 1 ;
}3 times
What is the value of i after this for loop?
int i; for ( i=10 ; i>0 ; i--) ;0
What's the output of this code:
int s=1, i=0;
while ( s<10 ) {
s += i;
i += 2;
}
printf( "%d %d" , i, s ) ;8 13
How many times does this while loop iterate?
int a = 3; while ( a = 2 ) printf("Hi") ;Infinite loop
What's the output of this code?
int i=5 ;
while ( i<20 ) {
if ( i%3 == 0) printf("%d ", i);
i +=2 ;
}9 15
What is the output of the following code?
int c = 5, n = 10 ;
do {
n /= c;
} while ( c-- ) ;
printf("%d", n) ;
Run-time Error
How many times does this while loop iterate?
int a = 0; while ( a = 0 ) printf("Bye") ;none
For an array of 10 elements, named 'arr', which of the following is the correct way of accessing the 5th element?
arr[4]
Which of the following statements are true about this array traversal code?
int s = 0 ;
int v[4] = { 35, 17, 28 };
for (int x = 0; x < 24; x++)
printf("%d",v[x]) ;This for loop will print the three items of the array followed by 21 garbage values.
This code will not compile because the for loop goes beyond the number of items in the array.
This for loop will print the three items of the array, followed by a single 0, then 20 garbage values.
The for loop will cause a run-time out-of-bounds error since it's accessing elements outside the range of the array.
This for loop will print the three items of the array, followed by a single 0, then 20 garbage values.
Which of the following statements is the syntax for declaring and initializing a 2D array in C?
int a[ 2 ][ 3 ] = { { 3, 5, 3 }, { 4, 1, 1 } };
int [][] a = { { 3, 5, 3 }, { 4, 1, 1 } };
int a[2][] = { { 3, 5, 3 }, { 4, 1, 1 } };
int [ 2 ][ 3 ] a = { { 3, 5, 3 }, { 4, 1, 1 } };
int a[ 2 ][ 3 ] = { { 3, 5, 3 }, { 4, 1, 1 } };
What of the following is NOT the correct syntax for declaring an array of integers?
int A = { 35, 2, 8, 17 } ;
int A[] = { 35, 2, 8, 17 };
int A[25] = { 35, 2, 8, 17 };
int A[4] = { 35, 2, 8, 17 };int A = { 35, 2, 8, 17 } ;
True or False: this code results in a syntax error because the loop goes beyond the last element of the array.
int a[] = { 1, 5, 6, 10 } ;
for(int i = 0; i < 100; i++ ) {
printf( "%d\n", a[ i ] );
} False
How many values will the following array hold?
int nums[5];
5
For this array:
int a[ 5 ] = { 5, 4, 3, 2, 1 };Which of the following for loops will generate the indices needed to traverse the array? Assume i is declared.
for( i = 1; i <= 5; i = i + 1 )
for( i = 0; i <= 4; i = i + 1 )
for( i = 0; i < 5; i = i - 1 )
for( i = 0; i <= 5; i = i + 1 )
for( i = 0; i <= 4; i = i + 1 )
True or False? The following code is how you're supposed to compare two strings?
if( str1 == str2 ) printf( "These two strings are equal.\n" );
False
What is the output of the following code?
char name[] = "kev\0in";
printf("%s",name);
kev
What is the value of the result at the end of this code block?
char str[] = { '1', '2', '3', '4', '\0' , '5', '6' };
int result = strlen( str );4
Why doesn't char t[] = {'h','i'}; behave as expected in printf("%s",t);
Because it lacks the terminating '\0'.
What is the value of the result at the end of this code block?
char str[ 100 ] = "Hello";
int result = strlen( str );5
What is the output of the following code?
printf("%d", strlen("123456"));6
What is the output of this program?
#include ‹stdio.h›
void printValue (int x) {
x = x + 5;
printf("%d"
, x) ;
}
int main() {
int num = 10;
printValue(num);
printf(“%d”, num);
return 0;
}
15 10
Why can't you use sizeof inside a function to determine the number of elements of an array that is passed as an argument to that function?
Because the array is passed as a pointer, and sizeof returns the size of the pointer, not the array.
What is the output of this program?
#include < stdio.h>
int square (int n) {
n = n*n ;
return n;
int main() {
int n = 3 :
n = square (n);
printf ("%d "
, n) ;
n = square (n);
printf ("%d ", n);
return 0;
}
9 81
What is the output of the following code?
#include ‹stdio.h>
void foo() {
static int
count = 0;
count++; printf ("%d
, count) ;
}
int main) {
foo() ;
foo() ;
foo();
return 0;
}
1 2 3
What's the output of this program?
#include <stdio.h>
int
add (int a, int b) {
return a + b;
}
int square(int n) {
return n
}
int main() {
int result = square(add (2, 3));
printf("%d\n"
, result);
return 0;
}
25
What's the output of this program?
#include < stdio.h>
void updateArray (int arr], int size) {
for (int i = 0; i ‹ size; it+) {
arr[i] += 10;
}
int main () {
int nums[] = {1, 2, 3};
updateArray (nums, 3); printf("%d %d %d\n"
, nums [®], nums [1], nums [2]) ;
return 0;
}
11 12 13
Which of the following statements about call-by-value in C is true?
Expressions cannot be passed directly; only constants are allowed.
All other answers are wrong.
Arguments can be expressions, as their evaluated results are passed as values.
Only variables can be passed as arguments.
Arguments can be expressions, as their evaluated results are passed as values.
What is the output of the following code?
#include < stdio.h>
int f(int);
int g(int);
int main(int argc, char" argvl]) {
int x = 5, y = 10;
y += f(x) + g(x);
printf ("%d", y);
y += f(x) + g(x);
printf ("%d", y);
return 0;
int f(int x) [
int y;
y = g(x);
return y;
int g(int x) [
int y = 10 ;
y+= 1;
return y+x;
42 74
What's the output of this program?
#include ‹stdio.h›
void square (int n) {
n = n*n ;
return n;
}
int main() {
printf("%d“, square(3));
return 0;
}
compilation error
What's the output of this code?
#include ‹stdio.h>
int update(int x) {
x = x + 10;
return x ;
}
int main() {
int a = 5;
update (a) ; update (a);
printf("%d\n", a);
return 0;
}
5
How are parameters passed in C?
pass by value
What is the call stack?
The region in memory where a function's local variables are allocated during a function call.
What does it mean to dereference a pointer?
Get the value stored in the memory address that the pointer is pointing to
From where is memory dynamically allocated?
heap
What does it mean when a pointer is pointing at NULL?*
It is not pointing at anything
What is a pointer in C?
variable that holds the memory address of another variable
What is the output of the following code?
int f( int *i, int *j{
*i = *i * *i;
*j = *j * *j;
return 42 ;
int main() {
int x=5, y=2;
int *p = &y ;
f( &x, p);
printf(“%d, %d”, x,y);
return 0;
}
25, 4
Consider these statements:
int value = 57;
int *ptr = ?????;
What is the correct expression to make the variable ptr point at the address of value?
&value
What does free() do?
Deallocates memory
Which of the following printf statements print out the memory address that the pointer variable ptr is point at?
printf."%p", ptr );
printf( "%lf", ptr);
printf "%d", *ptr);
printf("%d", (void *) ptr);
printf."%p", ptr );
Consider the following struct
typedef struct MyStruct (
int x; int y;
) MyStruct ;
Assume you have a pointer array that points to an array of MyStructs. Which of the following is the correct syntax for accessing the data member x?
array[i].x
Consider the following struct
typedef struct MyStruct (
int x;
int y;
} MyStruct ;
Which of the following is the correct statement for dynamically allocating an array of MyStructs? Assume there will be 10 elements.
MyStruct *arr = (MyStruct*) calloc( sizeof(MyStruct) * 10);
MyStruct array [10] - (MyStruct) malloc( sizeof(MyStruct));
MyStruct *arr = (MyStruct *) malloc( sizeof(MyStruct) * 10);
MyStruct *arr = (MyStruct ) malloc( sizeof(MyStruct) * 10);
MyStruct arr - (MyStruct ) malloc( sizeof(MyStruct) * 10);
What of the following is the correct usage of a typedef type for the following example?
typedef int * MylntegerPointer;
A. int x - 5;
MylntegerPointer * p = &x;
B. MyintegerPointer = 5;
C. int x = 5;
typedef MylntegerPointer p = &x;
D. int x - 5;
Mylnteger Pointer p - &x;
int x = 5;
MylntegerPointer p = &x;
When passing large structs to functions, how can you avoid the overhead of copying?
Pass a pointer to the struct instead of the struct itself.
fypedef struct Laptop (int ramgb ; ) Laptop ;
Laptop *P ;
Laptop getLaptop() ;
Laptop myLaptop s
Given the declarations above, which of the following expressions is not valid in C?
myLaptop.ram_gb
myLaptop->ram_gb
(getLaptop()).ram_gb
((P+2)).ram_gb
myLaptop->ram_gb
Which statement is correct?
Neither struct nor pointer copying is allowed in C.
Both always alias the same object in memory.
Both struct and pointer copies make independent objects.
struct copy (through assignment) makes a new object; pointer copy aliases the same object.
struct copy (through assignment) makes a new object; pointer copy aliases the same object.
typedef struct _A ( int a; char b; } A ;
A f(A z) {z.a++ ; return z;}
int main() (
A x = {16, ‘b’} ;
A y = f(x) ;
printf("%d %d\n", x.a, y.a);
return 0;
What's the output?
16 17
With Laptop *inventory = …; which is equivalent to inventory[i].brand?
(*(inventory + i)).brand
T/F A recursive is only allowed to have one base case
F
What does f(6) return?
int f (int m) {
if (m==0) return 0;
if (m % 3 ==0) return 3 + f(m - 1);
else return 1 + f(m-1);
}
10
What is the result the function call £ (10) with this recursive function?
int f(int a) {
return 1 + f(a-2);
}
runtime error
What does the following recursive function compute?
int f(int A [], int L, int R ) {
if (L ==R) return A[L];
int mid = (L+R)/2;
int left = f(A, L, mid);
int right = f(A, mid + 1, R);
return left > right ? right : left;
}
the minimum value in the array segment A[L,R];
Which of the following recursive functions correctly returns the sum of integers from 0 to n;
int sum (int n){ return n*n/2; }
int sum (int n){ return n<0 ? n + sum(n-1) : 0; }
int sum (int n){ return n ? n + sum(n-1) : n; }
int sum (int n){ return n ? n + sum(n+1) : n; }
int sum (int n){ return n ? n + sum(n-1) : n; }
Complete the recursive return expression so the function computes n° (n raised to the power k) :
int power (int n, int k) {
return k == 0? 1: /* fill here */ ;
}
n * power(n, k-1);