PROG 2: Midterm 2

Ace your homework & exams now with 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" _____________________ "%s,%lf,%s,%lf"

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

The following sscanf() is used to read in one line of data from a file: 1 char buffer[ 1000 ] = ????; 2 int x, y, z; 3 char w[ 100 ]; 4 sscanf( buffer, "%d,%[^,],%d %d", &x, w, &y, &z ); Which of the following strings can be parsed correctly by this sscanf() above? "125 Hello there 12 35" ________________________________ "125, Hello there, 12 35" ________________________________ "125, Hellothere, 12, 35"

"125,Hello there,12 35"

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"

What is the correct code for a macro to take in two parameters and evaluate to the lesser of the two values passed in? #define getLesserValue( x, y ) ( x < y ) ? x ? y #define getLesserValue( x, y ) ( x > y ) ? x : y #define getLesserValue( x, y ) ( x < y ) ? x : y

#define getLesserValue( x, y ) ( x < y ) ? x : y

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 ________________________________ #ifndef FILE_H #define FILE_H // code goes here # endif FILE_H ________________________________ #ifdef FILE_H #define FILE_H // code goes here # endif

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

You have a program that accepts two additional command line arguments: an output file name and input file name in that order. 1 int main( int argc, char *argv[] ) 2 { 3 if( argc == 3 ) 4 { 5 /* Open Files Here */ 6 } 7 return 0; 8 } Which of the following code correctly opens these files inmain() at /* Open Files Here */? 1 FILE *f1 = fopen( argv[ 1 ], "w" ); 2 FILE *f2 = fopen( argv[ 2 ], "r" ); _____________________________________________ 1 FILE *f1 = fopen( argv[ 2 ], "w" ); 2 FILE *f2 = fopen( argv[ 1 ], "r" );

1 FILE *f1 = fopen( argv[ 1 ], "w" ); 2 FILE *f2 = fopen( argv[ 2 ], "r" );

What is the value of argc in the main()'s parameters when executing this program with the following command line arguments? ./myprogram alpha 5 beta

4

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; _______________ Xyz m; m[ "x" ] = 20; m[ "y" ] = 50;

Xyz m; m[ x ] = 20; m[ y ] = 50;

Which of the main function's parameters hold the actual command line arguments? int main( int argc, char *argv[] ) argc argv int

argv

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

argv [ 3 ]

Which of the following does an object file NOT contain? machine code C code

c code

What is the type of argv? double char** char* int

char **

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 ); ______________________________ char str[ 100 ]; fscanf( ptr, "%s", str ); _____________________________ char str[ 100 ]; fscanf( ptr, "%s100", str );

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

What function should you call after you are done with a file that you have opened?

fclose()

What should the header files not contain? function definitions function declarations

function definitions

Assume fileIn1 is a pointer to an input file opened for reading, fileIn2 is a pointer to another input file opened for reading. The code reads one word from both files and prints them to standard output. 1 char str1[ 20 ], str2[ 20 ]; 2 int x, y; 3 while( ( x = fscanf( fileIn1, "%s", str1 ) ) == 1 && ( y = fscanf( fileIn2, "%s", str2 ) ) == 1 ) 4 { 5 printf( "%s\t%s\n", str1, str2 ); 6 } 7 /* HERE */ How do you check at /* HERE */ if fileIn1 has more words than fileIn2? if (x > y) ____________ if (x == y)

if (x > y)

Which of the following is true for a makefile? - makefile describes to the make command that how to compile the program - makefile is used to preform single-step compilation

makefile describes to the make command that how to compile the program

What is the term to describe compiling each source file separately?

modular compilation

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 - 72 - 38 - This code does not compile

24

What is the return value of when this function is called with getValue( 2 )? int getValue( int n ) { if( n >= 5 ) { return 1; } return n * getValue( n + 1 ); } 2 3 4 5 10 24

24

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

1

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 ); } - 3 - 7 - stack overflow - 4

3

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 ); } 0 stack overflow 10 14

10

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

12 9 6 3

What is the output of this code? #define VALUE 10; int main( int argc, char* argv[] ) { int x = 3 + VALUE; printf( "%d", x ); return 0; } - this code results in a run-time error - this code results in a complie-time error - 13 - 3

13

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

13 11 9 7 5 3 1 -1

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 D */

After we open a file with fopen(), what we should do before writing things to the file?

Check whether the file pointer equals to null

When working with files, what does EOF or eof mean?

End of file

According to zyBooks and general good code organization, where should all the code for a struct go?

In its own separate source and header files

What does sscanf do?

Parses a string

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

Given this code to read in a file named a.txt, what do we know about the structure of the content in a.txt? Assume that fileIn is an open pointer to a file. 1 char str1[ 20 ], str2[ 20 ]; 2 int num; 3 while( fscanf( fileIn2, "%s %d %s", str1, &num, str2 ) == 3 ) 4 { 5 printf( "%s %d %s\n", str1, num, str2 ); 6 } The file contains a repeating pattern of string, int, string, int separated by a space. ________________________________________________________________________ The file contains a repeating pattern of string, int, string separated by a cmma. ________________________________________________________________________ The file contains a repeating pattern of string, int, string separated by a space.

The file contains a repeating pattern of string, int, string separated by a space.

When parsing out the command line arguments passed into the main(), what is always in the first argument? - the first argument after the executable name - A list of alll arguments tht follow the name of the executable's name - The name of the executable

The name of the executable

what does scanf return?

The number of correct inputs based on its format specifier string

Our program has the following condition at the beginning of themain(). 1 int main( int argc, char *argv[] ) 2 { 3 if( argc == 4 ) 4 { 5 /* do something */ 6 } 7 return 0; 8 } What does this code imply? The program is expecting at least 4 command line arguments including the name of the executable _______________________________________________________________________ The program is expecting exactly 4 command line arguments including the name of the executable _____________________________________________________________________ The program is expecting exactly 4 command line arguments excludingthe name of the executable

The program is expecting exactly 4 command line arguments including the name of the executable

You have a program that will take in three command-line arguments, which includes the name of the executable. You want to check if the third command-line argument is equal to the word READ. Which set of code allows you to check this while staying in bounds of the argv array. if( argc >= 3 ) { if( strcmp( argv[ 3 ], "READ" ) == 0 ) { printf( "You passed in READ!\n" ); } } ___________________________________________________ if( argc >= 3 ) { if( strcmp( argv[ 2 ], "READ" ) == 0 ) { printf( "You passed in READ!\n" ); } } __________________________________________________

if( argc >= 3 ) { if( strcmp( argv[ 2 ], "READ" ) == 0 ) { printf( "You passed in READ!\n" ); } }

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

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

What of the following is the correct usage of a typedef type for the following example? typedef int * MyIntegerPointer; int x = 5; MyIntegerPointer * p = &x; ___________________________________________ int x = 5; typedef MyIntegerPointer p = &x; ___________________________________________ int x = 5; MyIntegerPointer p = &x; ___________________________________________ MyIntegerPointer = 5;

int x = 5; MyIntegerPointer p = &x;

In class we created the makefile with the name 'makefile'. What if the makefile is named 'cs1714'. How do you tell the make command to use 'cs1714' as the makefile?

make -f cs1714

To write data on screen using fprintf(), what is the FILE * that is used? stdin *f stdout none

stdout

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

string


Related study sets

Chapter 6: values, ethics, and advocacy PrepU questions

View Set

AP Macroeconomics: GDP, Inflation, Unemployment Quiz

View Set

Commonlit - MASLOW'S HIERARCHY OF NEEDS

View Set

Volume of cubes and rectangular prisms, Volume of rectangular prisms, Money

View Set

fundamentals chapter 32 : Skin integrity and wound care

View Set

Network Auth and Security Chapter 4

View Set