CS 1714 final

Pataasin ang iyong marka sa homework at exams ngayon gamit ang Quizwiz!

What is a struct? A) A collection of variables of different types B) A collection of variables of different types where only one variable can have a valid value C) An array of integers or doubles D) A set of similar functions

A collection of variables of different types

What is the output of the following code? int i=1, j=3; int arr[5] = {1,2,3,4,5}; printf ("%d", ++arr[i++] + arr[++j]++); A) 9 B) 8 C) 7 D) Garbage

8

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 ); } A) 8 B) 7 C) 10 D) 6

8

Which of the following is a 3-dimensional array? A) int a[10,10,10]; B) int a[10][10]; C) int a[10][10][10]; D) int a(10)(10)(10);

int a[10][10][10];

Which of the following is the reference operator? A) & B) / C) ++ D) *

&

For this array: int a[ 5 ] = { 5, 4, 3, 2, 1 }; What is the value of a[ 1 ] + a[ 2 ]? A) 7 B) 3 C) 9 D) Not enough information to calculate

7

Consider these statements int value = 57; int *ptr = ???????; what is the correct expression to make the variable ptr point at the address of the value? A) 57 B) &value C) *value D) value

&value

What is the return type of the function with prototype? "int test(char, float, double);" A) double B) char C) int D) float

int

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 ); A) 38 B) This code does not compile C) 24 D) 72

24

If you have an array of 100 values, what is the most number of comparisons to find a key value you will have to make in a binary search? A) 16 B) 32 C) 7 D) 8

7

What is the format specifier for a string? A) %c[] B) %lf C) %s D) %c

%s

Assume the pointer variable declaration: int *ptr; Which of the following expressions is an example of a dereferenced variable? A) *ptr B) ptr C) (ptr) D) &ptr

*ptr

What is the output of the following code? #include<stdio.h> #include<string.h> int main(int argc, char *argv[]) { printf("%d", strcmp("Hi","Hj")); return 0; } A) 0 B) 1 C) 2 D) -1

-1

If the two strings are identical, then strcmp() function returns A) 0 B) -1 C) 1 D) True

0

What is the output of the following code? #include<stdio.h> int function(); int main(int argc, char* argv[]) { int i; i = function(); printf("%d", i); return 0; } int function() { int a; a = 250; return 0; } A) 0 B) Run-time error C) 250 D) No output

0

What is the value of i after this for loop? int i; for( i = 10; i > 0; i-- ) { } A) 1 B) 0 C) -1 D) i is out of scope

0

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 ); A) 11 9 7 5 B) 9 7 5 3 1 C) 10 8 6 4 2 0 D) 10 8 6 4 2

10 8 6 4 2

What is the output of the following code? #include <stdio.h> int fun(int *num) { return (*num)--; } int main(int argc, char* argv[]) { int num = 16; for(fun(&num); fun(&num); fun(&num)) printf("%d ", fun(&num)); return 0; } A) 15 12 9 6 3 B) 13 10 7 4 1 C) Infinite loop D) 14 11 8 5 2

14 11 8 5 2

Given this array: int a[] = ( 99, 30, 88, 90, 15, 17, 13 ); After how many interations of the OUTER for-loop in Selection sort will result in the array have this sequence of values (from index 0 to 7)? 13, 15, 17, 90, 30, 88, 99 A) 1 B) 4 C) 3 D) 2

3

What are the contents in the STACK after the following code from top to bottom? push( 10 ); push( 20 ); pop(); push( 30 ); push( 40 ); pop(); A) 10, 20, 30, 40 B) 30, 10 C) 10, 30 D) 30, 40

30, 10

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 ); } A) Stack overflow B) 7 C) 3 D) 4

4

What is the greatest index number for the following array? int nums[5]; A) 6 B) 4 C) 5

4

What is the output of the following code? int i = 0; for (i = 0; i < 20; i++) { switch(i) { case '0': i += 1; case '1': i += 2; case '5': i += 3; default: i += 4; break; } printf("%d ", i); } A) 10 15 20 B) 4 9 14 19 C) 5 10 15 20 D) 1 4 9 14 19

4 9 14 19

Given this array: int a[] = { 28, 17, 13, 50, 30, 12 }; In Bubble sort, how many times does the outer for-loop iterate for this array? A) 6 B) 0 times C) 3 D) 5

5

If you had 50 nodes, what's the minimum height of a BST? A) 6 B) 15 C) 17 D) 5

5

Given this insertion order of values into a binary search tree: 100 150 50 80 40 20 90 60 If we were to remove node 50, which of the other nodes would be the replacement according to our removal algorithm presented in class? A) 60 B) 80 C) 100 D) 90 E) 40

60

What is an abstract data type? A) A model for analyzing data B) A model that describes how we will operate on the data C) A model for how many data members are needed D) It is exactly the same as a data structure

A model that describes how we will operate on the data

Which is the correct way to declare a for loop in C? A) int i; for( i = 0; i < 5; i++ ) { printf( "%d\n", i ); } B) for( i = 0; i < 5; i++ ) { printf( "%d\n", i ); } int i; C) for( int i = 0; i < 5; i++ ) { printf( "%d\n", i ); } D) int i; for( int i = 0; i < 5; i++ ) { printf( "%d\n", i ); }

A) int i; for( i = 0; i < 5; i++ ) { printf( "%d\n", i ); }

Consider this code to get user input for a string: char str[ 3 ]; scanf( "%s", str ); What happens if the user enters a string that is 100 characters long? A) The first two (2) characters are read into str with remaining characters still in the input buffer B) The first three (3) characters are read into str with remaining characters still in the input buffer C) The first two (2) characters are read into str with remaining characters flushed from the buffer D) The first three (3) characters are read into str with remaining characters flushed from the input buffer

A) The first two (2) characters are read into str with remaining characters still in the input buffer

What is the general form of fgets()? A) char* fgets(char* str, int num, FILE* stream); B) char* fgets(char* str, FILE* stream, int num); C) char* fgets(FILE* stream, char* str, int num); D) char* fgets(FILE* stream, int num, char* str);

A) char* fgets(char* str, int num, FILE* stream);

If the name of the header file is arrays.h, what would be an appropriate header guard name according to our coding style conventions? A) ARRAYS B) ARRAYS_H C) arrays_h D) arrays.h

ARRAYS_H

For any node in a BST, which of the following statements are NOT true? A) Any values in its right subtree, if they exist are greater than the node's value. B) If the node in the BST does not have a parent or ancestor node, it is the root node. C) Any values in its left subtree, if they exist are less than the node's value. D) Any values in the node's descendants are greater than the node's values

Any values in the node's descendants are greater than the node's values

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? A) At /* Line B */ B) At /* Line E */ C) At /* Line C */ D) At /* Line A */ E) At /* Line D */

At /* Line A */

In Selection Sort, when are values swapped? A) Every time a smaller value has a been found inside the inner for-loop B) At the beginning of the outer for-loop C) At the end of the outer for-loop D) At the beginning of the inner for-loop

At the end of the outer for-loop

In ANSI C (the traditional version), where are variables declared? A) At the bottom of a code block indicated by the { } B) As macros C) At the top of a code block indicated by the { } D) Anywhere in a code block as indicated by the { }, so long as it is within scope

At the top of a code block indicated by the { }

Given this array: int a[] = { 33, 51, 28, 17, 13, 50, 30, 12 }; What are the values of the array a (in order from index 0 to 8) after THREE (3) iterations of the outer for-loop in Bubble Sort? A) 33 28 17 13 50 30 12 51 B) 17 13 28 30 12 33 50 51 C) 13 17 28 12 30 33 50 51 D) 28 17 13 33 30 12 50 51

B) 17 13 28 30 12 33 50 51

Write a function to search a value in a binary search tree.

BSTNode* search(BSTNode* root, int v) { if(root == NULL || root->val == v) return root; else if(root->val > v) return search(root->left, v); else return search(root->right, v); }

What does this conditional statement print out? int a = 0; if( a = 0 ) { printf( "Alpha" ); } else { printf( "Beta" ); } A) Alpha B) This code does not compile C) This code does not print anything out D) Beta

Beta

Which of the following function correctly prints out a singly linked list in REVERSE? A) void printReverse( Node *h ) { Node *p; for( p = h; p != NULL; p = p->next ) { printf("%d\n", h->value ); } } B) void printReverse( Node *h ) { Node *p; for( p = h; p->next != NULL; p = p->next ) { printf("%d\n", h->value); } } C) void printReverse( Node *h ) { if( h == NULL ) return; printReverse( h->next ); printf( "%d\n", h->value ); } D) void printReverse ( Node *h ) { if( h == NULL ) return; printf( "%d\n", h->value ); printReverse( h->next ); }

C) void printReverse( Node *h ) { if( h == NULL ) return; printReverse( h->next ); printf( "%d\n", h->value ); }

Which of the following struct declarations is correct for a singly linked list? A) typedef struct Node { int value; struct Node *previous; struct Node *next; } Node; B) typedef struct Node { int value; Node *next; } C) typedef struct Node { int value; struct Node *next; } Node; D) typedef struct Node* { int value; } Node*;

C) typedef struct Node { int value; struct Node *next; } Node;

Given this array: int a[] = { 3, 2, 1, 0, 5, 7, 4 }; What are the values of the array a (in order from index 0 to 6) after TWO (2) iterations of the outer for-loop in Insertion Sort? A) 1, 2, 3, 0, 5, 7, 4 B) 0, 1, 2, 3, 4, 5, 7 C) 0, 1, 2, 3, 5, 7, 4 D) 2, 3, 1, 0, 5, 7, 4

C) 1, 2, 3, 0, 5, 7, 4

Given this array: int a[] = { 35, 3, 10, 40, 50, 17, 8 }; What are the values of the array a (in order from index 0 to 5) after ONE (1) iterations of the outer for-loop in Insertion Sort? A) 3, 10, 35, 40, 50, 17, 8 B) 35, 3, 10, 40, 50, 17, 8 C) 3, 35, 10, 40, 50, 17, 8 D) 3, 8, 10, 17, 35, 40, 50

C) 3, 35, 10, 40, 50, 17, 8

What is the output of the following code? #include<stdio.h> int main(int argc, char* argv[]) { int a = 25, b = 24 + 1, c; printf("%d", function(a, b)); return 0; } int function(int x, int y) { return (x - (x == y)); } A) Compile-time error B) 24 C) 25 D) Run-time error

Compile-time error

Assume that the h points to the head of a linked list. Which of the following linked list traversal for loops will correctly get ptr to point at the last item in the linked list? A) Node *ptr; for( ptr = h; ptr != NULL; ptr = ptr->next ) { } B) Node *ptr; for( ptr = h; ptr != NULL; ptr++ ); C) Node *ptr; for( ptr = h; ptr->next != NULL; ptr = ptr->next ); D) Node *ptr; for( ptr = h; ptr != NULL; ptr->next ) { }

D) Node *ptr; for( ptr = h; ptr != NULL; ptr->next ) { }

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

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

Given this array: int a[] = { 28, 17, 13, 50, 30, 12 }; What are the values of the array a (in order from index 0 to 5) after THREE (3) iterations of the outer for-loop in Bubble Sort? A) 17, 50, 28, 30, 12, 13 B) 28, 17, 13, 50, 30, 12 C) 12, 13, 17, 28, 30, 50 D) 13, 17, 12, 28, 30, 50

D) 13, 17, 12, 28, 30, 50

Given this array: int a[] = { 35, 3, 10, 40, 50, 17, 8 }; What are the values of the array a (in order from index 0 to 5) after TWO (2) iteration of the outer for-loop in Insertion Sort? A) 3, 8, 10, 17, 35, 40, 50 B) 3, 10, 17, 35, 40, 50, 8 C) 3, 10, 35, 40, 50, 8, 17 D) 3, 10, 35, 40, 50, 17, 8

D) 3, 10, 35, 40, 50, 17, 8

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. A) MyStruct array[ 10 ] = (MyStruct) malloc( sizeof(MyStruct) ); B) MyStruct *arr = (MyStruct) malloc( sizeof(MyStruct) * 10 ); C) MyStruct *arr = (MyStruct *) calloc( sizeof(MyStruct) * 10 ); D) MyStruct *arr = (MyStruct *) malloc( sizeof(MyStruct) * 10 );

D) MyStruct *arr = (MyStruct *) malloc( sizeof(MyStruct) * 10 );

Assume you have a linked list of nodes with an integer data member. Nodes are stored in ascending order with respect to that integer data member. In what order (in terms of the integer data member in the linked list nodes) will this function print the values assuming the initial call passes in the head pointer of the linked list? void printLinkedList( LLNode* h ) { if( h == NULL ) return; printLinkedList( h->next ); printf( "%d ", h->value ); } A) Random order B) Ascending order C) This function results in stack overflow D) Descending order

Descending order

What is one advantage that an array has over a linked list? A) An array can grow in size during the run time of the program B) An array's size is fixed at compile time C) An array can exist in stack D) Direct access to each element in the array

Direct access to each element in the array

Assume you are writing a function what will do the following - find a node and return the sum of its two immediate children -if a child does not exist, it will use 0 as that childs value - if the \node does not exist, return 0; Part of the function is written below. Which of the following code blocks will complete this function so that it will corectly complete the tasks above. int getChildrenSum( BSTNode *n, int key ) { if( n == NULL ) return0; if( n->value == key ) { /* what code goes here */ } return getChildrenSum( n->left, key ); return getChildrenSum( n->right, key); } A) if( n->left != NULL && n->right != NULL ) return n->left->value + n->right->value + n; B) int sum = 0; sum += n->left->value; sum += n->right->value; C) return n->left->value + n->right->value + n; D) int sum = 0; if( n->left != NULL ) sum += 0; if( n-> right != NULL ) sum += 0; return sum; E) int sum = 0; if( n->left != NULL ) sum += n->left->value; if( n->right 1= NULL ) sum += n->right->value; return sum;

E) int sum = 0; if( n->left != NULL ) sum += n->left->value; if( n->right 1= NULL ) sum += n->right->value; return sum;

What is the purpose of a header guard? A) Allows the header file to be included at the preprocessor stage B) Ensures that the compiler only includes the contents of the header file once. C) Joins each source file with its respective header file D) Prevents the compiler from including certain header files.

Ensures that the compiler only includes the contents of the header file once.

In vim, which of the following keys can take you from Insert mode to Command mode? A) Delete B) Escape C) Cmd D) $

Escape

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" ); } A) True B) False

False

True or false. A recursive is only allowed to have one base case. A) True B) False

False

What is a disadvantage of an unsorted an array in terms of searching for a value? A) There is an excellent chance that the first item in the array will be the value you are looking for B) Recursion will make searching the array much quicker C) In the worst case scenario, you only have check half the array D) In the worst case scenario, you may have to check every item in the array when searching for a specific value

In the worst case scenario, you may have to check every item in the array when searching for a specific value

What is a data structure? A) User defined data type consisting of constant values B) Preprocessor directives C) It is a way to store, organize, perform operations on data D) Any of the primitive data types, such as int or double

It is a way to store, organize, perform operations on data

In a function that does a preorder traversal and printing of a BST, when is the root/top node's value printed out? A) It is the last node printed B) It is skipped C) It is printed in the ascending sequence order D) It is the first node printed

It is the first node printed

What does it mean if malloc() returns a NULL? A) It was successful in allocating memory dynamically B) If malloc() returns NULL, you must use calloc() C) It was unable to dynamically allocate memory D) NULL is the first available memory address

It was unable to dynamically allocate memory

Which of the following is not a typical way to traverse a binary search tree? A) Law & Order B) Inorder C) Postorder D) Preorder

Law & Order

This function is supposed to traverse a tree and print its values out. void printTree( BTNode *node ) { //LINE A if( node == NULL ) { //LINE B return; } //LINE C printInorder( node->left ); //LINE D printInorder( node->right ); //LINE E } To print INORDER, at which line should printf( "%d", node->value); go? A) Line E B) Line B C) Line D D) Line C E) Line A

Line D

In vim, in Command mode, what does $ do? A) Moves to the end of the line B) Nothing C) Moves to the next occurrence of the character $ D) Moves to the beginning of the line

Moves to the end of the line

Assume that you are in the middle of linked list and that ptr is pointing a node. What is the correct code to remove the node AFTER ptr? A) Node *temp = ptr; ptr->next = ptr->next->next; free( ptr ); B) free( ptr->next ); C) Node *temp = ptr->next; ptr->next = ptr->next; free( ptr ); D) Node *temp = ptr->next; ptr->next = ptr->next->next; free( ptr );

Node *temp = ptr->next; ptr->next = ptr->next->next; free( ptr );

Write a function to append a node in a linked list. You can assume there is a createNode() function that takes in an integer and returns the new node.

Node* append(Node* head, int v) { Node* n = createNode(v); if(head == NULL) return n; Node* t = head; while(t->next != NULL) t = t->next; t->next = n; return head; }

What is it called when all nodes in a BST have exactly 2 child nodes except for the leaf nodes and all leaf nodes are on the same level? A) AVL B) Full C) Complete D) Perfect

Perfect

Which of the following is NOT true about typedef? A) Resolved at compile time B) Used for aliasing a type C) Simplifying any complex types (e.g., pointers, structs). D) Resolved at the pre-processing stage in the compilation process

Resolved at the pre-processing stage in the compilation process

What is the output of the following code? int c = 5, n = 10; do { n /= c; } while ( c-- ); printf("%d", n); A) 0 B) Run-time Error C) Compile-time Error D) 1

Run-time Error

In the absence of a exit condition in a recursive function, the following error is given A) None B) Run-time error C) Both D) Compile-time error

Run-time error

In recursion, what is the term to describe when the new stack frames for these recursive function calls extend beyond the allocated space in the stack? A) Stack exchange B) Stack overflow C) Stack frame D) Segmentation fault

Stack overflow

What is the result the function call function( 6 ); with this recursive function? int function( int n ) { return n + function( n - 3 ); } A) 3 B) 4 C) Stack overflow D) 6

Stack overflow

What is a base case in a recursive function? A) The initial call to the function B) When the function calls itself C) The simplest computation/task to be completed in a recursive function. D) When the function calls another function

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

What must be true for you to do a binary search on an array? A) The values must be sorted B) The values must numeric C) The values must be integers D) The values must be unsorted

The values must be sorted

This code is supposed to print out the value of the current node in a singly linked list as well as its previous node's value. Assume head is pointing to the head of a linked list. This code will result in a segmentation error. Why is that? 1 Node * current; 2 Node *previous; 3 4 for( current = head; current != NULL; current = current ->next ) 5 { 6 printf( "Previous %d\n", previous->value ); 7 previous = current; 8 printf( "Current %d\n", current ->value ); 9 } A) previous = ptr; needs to happen at the very end of the loop B) There is no previous at the head of the linked list and does not make the necessary checks for this case C) The loop goes beyond the last item of the linked list so there is no current D) This code is set up for a doubly linked list E) The initialization of the for loop must also include setting previous to head

There is no previous at the head of the linked list and does not make the necessary checks for this case

Which of the following statements are true about this array traversal code? int array[ 4 ] = { 35, 17, 28, 15 }; int x; for( x = 0; x < 24; x++ ) { printf( "%d\n", array[ x ] ); } A) This code will not compile as the format specifier is incorrect. B) This for loop will print the four items of the array followed by 20 garbage values. C) This code will not compile because the for loop goes beyond the number of items in the array. D) The for loop will stop iterating after printing the four items in the array.

This for loop will print the four items of the array followed by 20 garbage values.

What does the following code print out? int x; printf( "%d", x ); A) Who knows? It's garbage. B) -1 C) 0 D) Error: you can't print out a value without initializing it

Who knows? It's garbage.

In the main()'s parameters, how do I access the argument gamma.txt? ./myprogram alpha.txt beta.txt gamma.txt A) argv[ 3 ] B) argv[ 2 ] C) argv[ 4 ] D) argv

argv[ 3 ]

What data structure matches with this description? A data structure were values are stored in a consecutive manner and directly accessible by its positional index A) hash table B) linked list C) array D) binary tree

array

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? A) array[ i ]->x B) array[ i ].x C) array[ x ] D) array->i->x

array[ i ].x

Which of the following is not a primitive data type in C? A) boolean B) int C) char D) double

boolean

In Linux, what is the command to change directory? A) changed B) change directory C) dir D) cd

cd

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? A) char str[ 100 ]; fscanf( ptr, "%s", str ); B) char str[ 100 ]; fscanf( ptr, "%s", &str ); C) char str[ 100 ]; fscanf( ptr, "%s100", str ); D) char str[ 100 ]; fscanf( infile, "%100s", str );

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

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? A) foobar( 20, 20 ); B) foobar( 20, 19 ); C) foobar( 35, 20 ); D) foobar( 20, 30 );

foobar( 20, 30 );

For this array: int a[5] = { 5, 4, 3, 2, 1 }; Which of the following for loops will generate this indices needed to traverse the array? Assume i is declared A) for(i=0; i<=4; i=i+1) B) for(i=0; i<5; i=i-1) C) for(i=0; i<=5; i=i+1) D) for(i=1; i<=5; i=i+1)

for( i=0; i<= 4; i= i + 1 )

How do we compile our C file with gcc when the math.h library is included and you are using functions such as pow or sqrt? A) gcc program.c -o program B) gcc program.c -o program #include <math.h> C) gcc program.c -o program math D) gcc program.c -o program -lm

gcc program.c -o program -lm

Assume you have a singly linked list with these values: head -> 30 -> 70 -> 80 -> 100 -> 120 -> 150 -> NULL with this function: 5 Node *doMagicIotaOne( Node *h ) 6 { 7 if( h == NULL ) 8 return NULL; 9 10 if( h->value == 120 ) 11 return NULL; 12 13 return doMagicIotaOne( h->next ); 14 } What are the contents of the linked list that head is pointing at after calling this function with this function call? head = doMagicIotaOne( head ); A) head -> 30 -> 70 -> 80 -> 100 -> 150 -> NULL B) head -> 30 -> 70 -> 80 -> 100 -> NULL C) head -> NULL D) head -> 120 -> 150 -> NULL E) head -> 30 -> 70 -> 80 -> 100 -> 120 -> 150 -> NULL

head -> NULL

Write a recursive function to calulate the sum of digits for a given integer

int sumDigits(int n) { if(n == 0) return 0; return n%10 + sumDigits(n/10); }

What is the output of the following code? char name[6] = "kev\0in"; printf("%s",name); A) ERROR B) kevin C) kev D) kev\0in

kev

Where is the entry point of a C program? A) main() function B) The first listed function in a .c file C) The first line of the .c file D) A random function is selected

main() function

which of the following is true for a makefile? A) makefile is a default file provided by gcc, and is not written by the user B) makefile is used to perform single-step compilation C) makefile can be run directly as a normal executable file - ./makefile D) makefile describes to the make command that how to compile the program

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

In Linux, what is the command to create a directory? A) cd B) mkdir C) cp D) crdir

mkdir

Which of the following printf statements print out the memory address that the pointer variable ptr is point at? A) printf( "%d", *ptr ); B) printf( "%lf", ptr ); C) printf( "%p", ptr ); D) printf( "%d", (void *) ptr );

printf( "%p", ptr );

Create a makefile for a project with the files and the dependencies as mentioned below and the executable output filename as 'project'. main.c -> point.h, mesh.h mesh.c -> mesh.h, point.h point.c -> point.h

project: main.o mesh.o point.o gcc main.o mesh.o point.o -o project main.o: main.c mesh.h point.h gcc -c main.c mesh.o: mesh.c mesh.h point.h gcc -c mesh.c point.o: point.c point.h gcc -c point.c clean: rm *.o project

The keyword used to transfer control from a function back to the calling function is A) while B) for C) return D) switch

return

What functions can you use to have a user input a string? A) scanf() B) fgets() C) input() D) fscanf()

scanf() fgets() fscanf()

what header file must be included to use malloc()? A) stdio.h B) string.h C) stdlib.h D) math.h

stdlib.h

Consider these two string variables: char str1[] = "computer"; char str2[20]; How do you assign str1 to str2 so that both strings have the same characters? A) str2 = str1 B) strcpy( str1, str2 ) = 0 C) strcpy( str2, str1 ); D) strcpy( str1, str2 );

strcpy( str2, str1 );

Consider this struct declaration WITHOUT a typedef. struct Coordinates { double latitude; double longitude; }; What is now the correct syntax for using this struct? A) struct Coordinates c; B) Coordinates c; C) struct *c = Coordinates; D) struct * Coordinates c;

struct Coordinates c;

Write code to read student data from a csv file and PREPEND that into a linked list. Assume the code is written in only one file - main.c. - You are REQUIRED to write the struct definition for the linked list node. - The input file name is taken from command line argument. - You can write everything except the struct definition in the main function, or can create multiple functions - up to you. Following is the sample file data. 1002,John,3.5 1003,Steve,2.5 1001,Mary,3.0 1004,Sarah,2.8

typedef struct Student { int id; char name[10]; double gpa; struct Student* next; } Student; int main(int argc, char* argv[]) { if(argc != 2) { printf("Usage: ./exe inputFile.csv \n"); return -1; } FILE* inFIle; inFile = fopen(argv[1], "r"); if(inFile == NULL) { printf("Error opening file\n"); return -1; } char buffer[1000], name[10]; int id; double gpa; Student* head; Student* t; while(fgets(buffer, 100, inFile)) { sscanf(buffer, "%d,%[^,],%lf", &id, name, &gpa); t = (Student*)malloc(sizeof(Student)); t->id = id; strcpy(t->name, name); t->gpa = gpa; t->next = head; head = t; } fclose(inFile); return 0; }

What is the output of the following code? #include<stdio.h> #include<string.h> int main(int argc, char *argv[]) { char str1[] = "hello world"; char str2[] = "hello world"; if (strcmp(str1, str2)) printf("equal"); else printf("unequal"); } A) unequal B) run-time error C) equal D) compile-time error

unequal

Write a function that prints the following pattern, based on the value of the parameter N. If N = 3, it should print * ** *** If N = 4, it should print * ** *** ****

void printPattern(int N) { int i=0, j=0; for(i=1; i<=N; i++) { for(j=1; j<=i; j++) { printf("*"); } printf("\n"); } }


Kaugnay na mga set ng pag-aaral

The Skilled Helper (11th edition) Chapter 3

View Set

SB CH. 9 - Sales Order Processing

View Set

Integumentary Disorders Practice Questions

View Set

Computer Science Basics & Hardware

View Set

F5-M6: Troubled Debt Restructuring and Extinguishment

View Set

Tema 8. Organización / Admin / 1º / Uni

View Set