Practice Exam 2

¡Supera tus tareas y exámenes ahora con Quizwiz!

Assume that you have an external CSV file that has this structure: First-Name,35,Last-Name,5.392 and you want to use fscanf() to parse out the string. What is the format specifier string that will be used to parse this row out?

"%s,%d,%s,%lf"

What is the correct specifier in fopen() to open a binary file for reading?

"rb"

What is the correct specifier in fopen() to open a binary file for writing?

"wb"

Assume that you have a header file named file.h, what is the correct way to define the header guards?

#ifndef FILE_H #define FILE_H // code goes here # endif

What does doMagicBeta( 6 ); return? int doMagicBeta( int m ) { if( m < 0 ) return 0; if( m % 2 == 0 ) return 1 + doMagicBeta( m - 2 ); else return 3 + doMagicBeta( m - 1 ); }

4

What is the return value of when this function is called with doMagic( 10, 4 )? int doMagic( int x, int y ) { if( x < y ) { return 1; } return 1 + doMagic( x - 1, y ); }

8

What is a struct?

A collection of variables of different types

What does fopen() return if it was successful in opening a file?

A pointer to the file

What is the output of this code? #define VALUE 10; int main( int argc, char* argv[] ) { int x = VALUE * 2; printf( "%d", x ); return 0; }

This code results in a compile-time error.

What does fclose() do?

When done using the file, the program closes the file.

Under what circumstances will make recompile code?

When it determines a dependent source, header, or object file was modified.

Consider this struct: typedef struct Xyz { int x; int y; } Xyz; We want to create an Xyz object where x is 20 and y is 50. Which of the following will initialize this struct correctly?

Xyz m; m.x = 20; m.y = 50;

In the main()'s parameters, how do I access the argument gamma.txt? ./myprogram alpha.txt beta.txt gamma.txt

argv[ 3 ]

Assume you have a FILE pointer named ptr which is pointing to an opened file. Which of the following statements is the correct way to read in a string?

char str[ 100 ]; fscanf( ptr, "%s", str );

For a text file, we need to count the number of times each letter appears in it. Which file reading function is most suitable for this task?

fgetc()

Which of the following is the correct code to open a file for writing? FILE * fileIn;

fileIn = fopen( "nameoffile.txt", "w" );

Consider this recursive function void foobar( int n, int m ) { if( m > n ) return; printf( "%d %d\n", n, m ); foobar( n, m - 2 ); } Which of the following function calls will NOT result in a stack overflow?

foobar( 20, 30 );

For a text file, we want to calculate how many words there are, and we want to read in one word each time. Which file reading function is most suitable for this task?

fscanf()

If you have a struct to hold information a 2D coordinate, which of the following declarations are correct?

typedef struct Coordinate { double x; double y; } Coordinate;

If you have a struct to hold information about a person's name, phone number, and email address, which of the following declarations are correct?

typedef struct Person { char name[ 100 ]; int phone; char email[ 100 ]; } Person;

Given this macro: #define doSomething( x, y ) x + y * 2 What is the value of result after this code? int result = 2 * doSomething( 5, 7 );

24

Which of the following is NOT a descriptor of a makefile?

It can split a source file into several object and header files.

What does feof() returns if end of file was reached?

1

What does doMagicAlpha( 6 ); return? int doMagicAlpha( int m ) { if( m == 0 ) return 0; if( m % 3 == 0 ) return 3 + doMagicAlpha( m - 1 ); else return 1 + doMagicAlpha( m - 1 ); }

10

Given this recursive function: void recurse( int n ) { printf( "%d ", n ); if( n < 3 ) { return; } else { recurse( n - 2 ); } } What is the print out that results from this function call? recurse( 10 );

10 8 6 4 2

doMagicBeta() is a recursive function that is supposed to print out an array in reverse order. 1 void doMagicBeta( int *array, int number ) 2 { 3 /* Line A */ 4 if( number == 0 ) 5 { 6 /* Line B */ 7 return; 8 /* Line C */ 9 } 10 else 11 { 12 /* Line D */ 13 number = number - 1; 14 doMagicBeta( array, number ); 15 } 16 /* Line E */ 17 } The initial call to this function will pass in an array of integers and the last valid index of the array (size minus one). Where do we put the following printf( "%d\n", array[ number ] ); statement to print out the entire array in reverse order?

At /* Line A */

Which of the following does an object file NOT contain?

C code

What is the value of the file pointer if the the file does not exist?

NULL

What does the following fgets() expression do assuming fileIn points to an existing file for input? char buffer[ 1024 ]; fgets( buffer, 1024, fileIn );

Reads in a single string up to 1024 characters or the new line character, which ever comes first

In the absence of a exit condition in a recursive function, the following error is given

Run-time error

What is the result the function call function( 6 ); with this recursive function? int function(int n) { return n + function(n-2); }

Stack overflow

What is wrong with this code for macro? #define doSomething ( x , y ) x + y // this macro adds the two parameters together int m = doSomething( 10, 30 );

The comment in the macro will take anything after the macro call out of compilation. In the assignment statement that follows, it comments out the semi colon

When parsing out the command line arguments passed into the main(), what is always in the first argument?

The name of the executable

What is a base case in a recursive function?

The simplest computation/task to be completed in a recursive function.

What happens if you pass in more command line arguments that the program is set up to handle?

They are ignored.

This recursive function does not have an explicit base case. void foobar( int n, int m ) { if( n <= m ) { printf( "%d %d\n", n, m ); foobar( n + 1, m ); } } If you had to change this code to include an explicit base case, what would that be?

if( n > m ) return;

What is the if-statement equivalent of this ternary expression? int m = ( x == 5 ) ? 15 : x;

int m; if( x == 5 ) m = 15; else m = x;

Consider the following struct typedef struct MyStruct { int x; int y; } MyStruct; Which of the following is the correct syntax for accessing a data member of a dynamically allocated struct pointer object named m?

m->x;

As we type "make" command on the terminal

make reads the makefile in the current directory

What is the type of any command line arguments passed into the main()?

string


Conjuntos de estudio relacionados

Developmental Psychology Chapter 12 Terms

View Set

Assignment 7 (Lesson 8) - WILEY Muscles and Muscular System

View Set

Chapter 55: Anticoagulant, Antiplatelet, and Thrombolytic Drugs Burchum: Lehne’s Pharmacology for Nursing Care, 11th Edition

View Set

PL Naturalization questions 1-30

View Set

Chapter 6 - Professional Liability and Medical Malpractice

View Set