prog c another FREAKING EXAM

0.0(0)
Studied by 0 people
call kaiCall Kai
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
GameKnowt Play
Card Sorting

1/71

encourage image

There are no tags or description

Looks like no tags are added yet.

72 Terms

1
New cards

What does this code print out?

double x = 12.482168;

printf( "%.1lf", x );

12.5

2
New cards

What is the format specifier for printf when printing a double?

%lf

3
New cards

Where is the entry point of a C program?

main() function

4
New cards

What does the following code print out?

int x;

printf( "%d", x );

garbage

5
New cards

In C, what is the size in bytes of a char?

1

6
New cards

What function is used to get user input from the standard input (usually the keyboard) ?

scanf

7
New cards

What is the escape character for a tab?

\t

8
New cards

In order to use math functions, which header file must be included?

math.h

9
New cards

What's the value of the expression: 5 / 2 * 2.0 ?

4.0

10
New cards

T/F: All C variables must be declared at the top of the block (function declaration, etc)

false

11
New cards

T/F: Declaration, allocation, and initialization of a variable are always done together. You cannot do one but not the other(s). 

F

12
New cards

What causes overflow in some integer expressions ?

Because the result exceeds the maximum (or minimum) value representable by the data type.

13
New cards

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.

14
New cards

When does the C compiler add implicit type conversion between integers and floats?

When an integer and a float appear in the same expression.

15
New cards

consider the following statement:

if (X) if (Y) S1 else S2 else S3

Which of the following sentences is correct?

S2 will be executed when X is true and Y is false

16
New cards

What gets printed ?

int a = 0;

if (a = 5) printf( "Alpha" ); else printf( "Beta" );

Alpha

17
New cards

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.

18
New cards

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.

19
New cards

How many times does this while loop iterate?

  int a = 2 ;
  while ( a >= 0 ) {
    a = a - 1 ;
  }


3 times

20
New cards

What is the value of i after this for loop?

int i; for ( i=10 ; i>0 ; i--) ;


0

21
New cards

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

22
New cards

How many times does this while loop iterate?

  int a = 3; while ( a = 2 ) printf("Hi") ;


Infinite loop

23
New cards

What's the output of this code?

int i=5 ;
while ( i<20 ) {
  if ( i%3 == 0) printf("%d ", i);
  i +=2 ;
}


9 15

24
New cards

What is the output of the following code?

  int c = 5, n = 10 ;
  do { 
    n /= c;
  } while ( c-- ) ;
  printf("%d", n) ;


 

Run-time Error

25
New cards

How many times does this while loop iterate?

int a = 0; while ( a = 0 ) printf("Bye") ;


none

26
New cards

For an array of 10 elements, named 'arr', which of the following is the correct way of accessing the 5th element?

 

arr[4]

27
New cards

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.

28
New cards

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 } };

29
New cards

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 } ;

30
New cards

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

31
New cards

How many values will the following array hold?

 

int nums[5];


 

5

32
New cards

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 )

33
New cards

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

34
New cards

What is the output of the following code?

char name[] = "kev\0in";
printf("%s",name);


 

kev

35
New cards

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

36
New cards

Why doesn't char t[] = {'h','i'}; behave as expected in printf("%s",t); 

 

Because it lacks the terminating '\0'.

37
New cards

What is the value of the result at the end of this code block?

char str[ 100 ] = "Hello";
int result = strlen( str );


5

38
New cards

What is the output of the following code?

printf("%d", strlen("123456"));


6

39
New cards

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

40
New cards

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.

41
New cards

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

42
New cards

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

43
New cards

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

44
New cards

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

45
New cards

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.


46
New cards

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

47
New cards

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

48
New cards

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

49
New cards

How are parameters passed in C?

pass by value

50
New cards

What is the call stack?

The region in memory where a function's local variables are allocated during a function call.


51
New cards

What does it mean to dereference a pointer?

Get the value stored in the memory address that the pointer is pointing to


52
New cards

From where is memory dynamically allocated?

heap

53
New cards

What does it mean when a pointer is pointing at NULL?*

It is not pointing at anything


54
New cards

What is a pointer in C?

variable that holds the memory address of another variable


55
New cards

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

56
New cards

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

57
New cards

What does free() do?

Deallocates memory

58
New cards

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 );

59
New cards

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


60
New cards

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);


61
New cards

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;

62
New cards

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.

63
New cards

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

64
New cards

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.


65
New cards

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

66
New cards

With Laptop *inventory = …; which is equivalent to inventory[i].brand?

(*(inventory + i)).brand

67
New cards

T/F A recursive is only allowed to have one base case

F

68
New cards

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

69
New cards

What is the result the function call £ (10) with this recursive function?

int f(int a) {

return 1 + f(a-2);

}

runtime error

70
New cards

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];

71
New cards

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; }

72
New cards

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);