Practice Exam 1

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

What is the syntax for declaring a variable with this struct? struct Place { char name[ 100 ]; char address[ 1000 ]; };

struct Place c;

Write code for the struct header file named "product.h" that consists of the following: header guard struct definition for Product which consists of 3 things: name (string of 10 characters), price (double value) and quantity (integer value) function prototypes for the following 3 functions: fillProduct() - The function will take in two parameters: array by reference and the length of the array. The function will not return anything. The function will initialize all the quantity values to be equal to the (length - index) * 10 . The price values will be equal to (index * 2.5) . All product names should be initialized to "sample". calculateAveragePrice() - The function will take in two parameters: array by reference and the length of the array. It will calculate the average price of all products, without considering the quantity of each product. This average value will be returned back by the function. totalPrice() - The function will take in three parameters: array by reference, length of the array, and the double totalPrice value passed as a pointer which will be updated with the calculated value. The function will not return anything. The totalPrice value should be updated to contain the weighted sum of the total price of all prices considering the quantity of each product as well.

#ifndef PRODUCT_H #define PRODUCT_H typedef struct Product { char name[10]; double price; int quantity; } Product; void fillProducts(Product[], int); double calculateAveragePrice(Product[], int); void totalPrice(Product[], int, double*); #endif;

What does it mean to dereference a pointer?

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

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

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

double char float ->int

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 = 0; i <= 4; i = i + 1 )

Which of the following is a 3-dimensional array?

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

Consider that the above Product files are already present and you are writing the code below in the main() function. Write code to for the following: -Create an integer N, which will be the number of products. -Take the value of N from the user. -Dynamically allocate memory for an array of N Product objects.

int n=0; scanf("%d", &n); Product* p = (Product*)malloc(sizeof(Product)*n);

In Linux, what is the command to full path of your current working directory?

pwd

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

Write code for the struct source file named "product.c" that consists of the following: includes for the header files function definitions for the following 3 functions: fillProduct() - The function will take in two parameters: array by reference and the length of the array. The function will not return anything. The function will initialize all the quantity values to be equal to the (length - index) * 10 . The price values will be equal to (index * 2.5) . All product names should be initialized to "sample". calculateAveragePrice() - The function will take in two parameters: array by reference and the length of the array. It will calculate the average price of all products, without considering the quantity of each product. This average value will be returned back by the function. totalPrice() - The function will take in three parameters: array by reference, length of the array, and the double totalPrice value passed as a pointer which will be updated with the calculated value. The function will not return anything. The totalPrice value should be updated to contain the weighted sum of the total price of all prices considering the quantity of each product as well.

#include<stdio.h> #include<string.h> include"product.h" void fillProducts(Product p[], int n) { int i=0; for(i=0; i<n; i++) { strcpy(p[i].name, "sample"); p[i].price = i*2.5; p[i].quantity = (n-i)*10; } } double calculateAveragePrice(Product p[], int n) { double sum=0; int i=0; for(i=0; i<n; i++) { sum += p[i].price; } return sum/n; } void totalPrice(Product p[], int n, double* totalPrice) { *totalPrice = 0; int i=0; for(i=0; i<n; i++) { *totalPrice += (p[i].price * p[i].quantity); } }

Consider these statements: int value = 57; int *ptr = ?????; What is the correct expresssion to make the variable ptr point at the addresss of value?

&value

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

->-1 1 2 0

What are the contents of array after this code? 6 int array[ 6 ] = { 10, 20, 30, 40, 50, 60 }; 7 int i; 8 for( i = 0; i < 6; i++ ) 9 { 10 if( i % 2 == 1 ) 11 *( array + i ) = 3; 12 } 13 *array = 100;

100, 3, 30, 3, 50, 3

What are the contents of the numbers array at the end of this code? int numbers[] = { 35, 57, 78, 66, 41, 12 }; int *ptrA = numbers; int *ptrB = ptrA + 2; *ptrB = 100; *ptrA = 100;

100, 57, 100, 66, 41, 12

How many values will the following array hold? int nums[5];

5

What is the output of the following code? #include<stdio.h> int fun(int); int main(int argc, char* argv[]) { int i = fun(10); printf("%d\n", --i); return 0; } int fun(int i) { return (i++); }

9

What is a pointer in C?

A variable that holds the memory address of another variable

What is the output of the following code? #include<stdio.h> int main(int argc, char* argv[]) { function(); return 0; } void function() { printf("Function in C is awesome"); }

Compile-time error

What is the output of the following code? int n; for( n = 9; n!=0; n-- ) { printf("%d ", n--); }

Infinite Loop

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 *) malloc( sizeof(MyStruct) * 10 );

Which of the following is NOT true about typedef?

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

Run-time Error

What is the value of result? int result = 13 == 12;

This code does not compile 12 False ->0

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

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

Does this code compile in C? int a[] = { 3, 2, 5 }; int i; for( i = 0; i < 100; i++ ) printf( "%d", a[ i ] );

Yes

Write a function definition that prints the following pattern, based on the value for N taken as an input parameter. If N = 3, it should print * * * * * * If N = 4, it should print * * * * * * * * * *

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

Which of the following is a valid and complete function definition?

void test (x) { printf ( "Hello" ); }


Conjuntos de estudio relacionados

Geography IB- Option E: Leisure, Tourism and Sport Case Studies

View Set

Java Collections and Data Structures

View Set

NWM Guaranteed Exam Health and Life

View Set

Postwar America Topic 8 Assessment

View Set

Lesson 1 Multiplication & Division Flashcards 1-10

View Set