CS1 Quiz 1
A string is terminated by a Null character, which is written as ________.
'\0'
What would be the output for the following program? int main(){ int i = -10, *j, **k; j = &i; k = &j; printf(" \n %d %d %d %d", i, *(&i), *j, **k); return 0; }
-10 -10 -10 -10
Consider a file pointer fp is being used in a file "data.txt" for reading. Which of the following statements can bring the file pointer to the beginning of the file?
-fclose(fp); -fp = fopen("data.txt", "r"); -rewind(fp) -fseek(fp, 0, SEEK_SET)
What is the output of the following program? int main(){ int b [ ] = { 0, 20, 0, 40, 5 }; int i, *k, *ar; k = b; ar = b; for( i = 0; i <= 4; i++){ printf(" \n%d %d", *k, ar[i]); k ++; } return 0; }
0 0 20 20 0 0 40 40 5 5
"A" is a _1______ while 'A' is a _2_________.
1.) string 2.) character OR char
Using big-oh notation, give the worst-case runtime for inserting a new element into a binary search tree with n nodes
O(n)
What would be the output of the following program? Before answering, you need to know whether a structure is passed by reference or passed by value? struct Point{ int x; int y; }; struct Point add(struct Point p, int i, int j) { p.x += i; p.y += j; return p; } int main() { struct Point p1 = {0,0}; p1 = add (p1, 1, 1); printf("%d %d", p1.x, p1.y); }
1 1
What is the Output of below Program? (Ignore the '\n'). Note that the line P1[2] = {0, 0, 0, 0} is equivalent to p1[0].x = 0; p1[0].y = 0; p1[1].x = 0; p1[1].y = 0; To solve this problem you need to know passing a structure array to a function is passed by reference or passed by value. struct Point{ int x; int y; }; void add(struct Point p[ ], int a, int b){ for(int i = 0; i < 2; i++){ p[ i ].x += a; p[ i ].y += b; } } void add2(struct Point *p, int i, int j){ p->x += i; p->y += j; } int main(){ struct Point p1[ 2 ] = {0, 0, 0, 0}; add(p1, 1, 1); for(int i = 0; i < 2; i++){ printf("%d %d\n", p1[ i ].x, p1[ i ].y; } return 0; }
1 1 1 1
What is the output of the following program? int main(){ int b [ ] = { 10, 20, 30, 40, 50}; int i; for(i = 0; i <= 4; i++){ printf(" \n%d %d", *(b + i), b[ i ] ); } return 0; }
10 10 20 20 30 30 40 40 50 50
What is the Output of the below Program? typedef struct X { int num; int *y; } X; int main(){ X x; X *p; p = &x; x.num = 10; x.y = &x.num; //consider the address is 104 printf("%d %d %u %u %u %u %d %d", p->num, (*p).num, &x.num, x.y, p->y, *(x.y), *(p->y)); return 0; }
10 10 104 104 104 10 10
What would be the output from the first printf? If the number is a floating point number, write it up to two decimal places. int main(){ float a = 13.5; float *b, *c; b = &a; //suppose address of a is 1006 c = b; printf(" \n%u %u %u", &a, b, c); printf(" \n%f %f %f %f", a, *(&a), *&a, *b, *c); return 0; }
1006 1006 1006
In the loop with the line printf, what would be printed when i = 2? int main(){ int a[5], i, b = 16; for(i = 0; i < 5; i ++){ a[ i ] = 2 * i; } f( a, b); for( i = 0; i < 5; i++){ printf(" \n%d", a[ i ]; printf(" \n%d", b); } void f ( int *x, int y ){ int i ; for( i = 0; i < 5; i++) *(x + i) += 2; y += 2; }
6
What would be the result in the printf of the following program? int main(){ int n[ 3][ 3 ] = { 2, 4, 3, 6, 8, 5, 3, 5, 1 }; printf(" \n%d %d %d", *n, n[ 3 ][ 3 ], n[ 2 ][ 2 ] ); return 0; }
<base Address> <garbage value/segmentation fault> 1
What is the error in the below program? int main(){ FILE fp; fp = fopen("data.txt", "r"); fclose(fp); return 0; }
File fp;
What of the following are true for the following piece of code? int main(){ int a[ ] = { 2, 4, 6, 8, 10}; int i; change( a, 5); for (i = 0; i <= 4; i++){ printf(" \n%d", a[ i ] ); } return 0; } void change(int *b, int n){ int i; for(i = 0; i < n; i++){ *(b + i) = *( b + i ) + 5; } }
It will add 5 to each array element. and display it
What is the error (if any) in this program? int main(){ int p = 23, f = 24; jiaayjo( &p, &f); printf(" \n%d %d", p, f); return 0; } void jiaayjo( int q, int g){ q = q + q; g = g + g; }
It will compile but there will be warning at line void jiaayjo(int q, int g) as q and g are not pointers and can't hold addresses
Using big-oh notation, give the best-case runtime for inserting a new element into a binary search tree with n nodes
O(1)
What is the error in the below Program? struct s{ char ch; int i; float a; }; int main(){ struct s var = {'C', 100, 12.55}; f(var); g(&var); return 0; } void f(struct s v){ printf(" \n%c %d %f", v->ch, v->i, v->a); } void g(struct s *v){ printf(" \n%c %d %f", v.ch, v.i, v.a); }
Pointer( -> ) and Dot(.) are wrongly used.
Which of the following is an example of nested structure? a) struct address{ char city[20]; int pin; char phone[14]; }; struct employee{ char name[20]; struct address add; }; b) struct employee{ char name[20]; struct address { char city[20] int pin; char phone[14]; }; }; c) struct address{ char city[20]; i int pin; }; struct employee{ char name[20]; char city[20]; int pin; }; d) None of the above.
a
Consider the following piece of code. An int takes 4 bytes of memory. What would be the value of a, b, and b-a? int X[5] = {10, 20, 30, 40, 50}; //consider the base address be 2000 int *a = X; int *b = &X[2];
a is2000, b is 2008, b-a is 2
What would be the output of the following program? int main(void){ char arr1[ 10 ] = "test1"; char arr2[ 20 ] = "Test1"; if(strcmp(arr1, arr2) < 0) printf("arr1 smaller"); else if(strcmp(arr1, arr2) == 0) printf("Same"); else printf("arr1 is larger"); return 0; }
arr1 is larger
which of the following is correct way of opening a file? a) int main(){ FILE *t; char filename[30]; scanf("%s", filename); t = fopen(filename, "r"); fclose(t); return 0; } b) int main(){ FILE *t; t = fopen("main.c", "r"); fclose(t); return 0; }
both
What is the output of the following program? int main( ) { char s[ ] = "cat" ; int i = 0 ; while ( s[i] != '\0' ) { printf ( "%c%c ", s[i], *( s + i ) ) ; i++ ; } }
ccaatt OR cc aa tt
A string is represented as an array of characters. If you need to store an array of 5 strings with the maximum length of a string is 100, how would you declare it?
char str[5][101];
The array elements are always stored in __________ memory locations. Write the word consecutive or random in the blank appropriately
consecutive OR continuous
Which of the following programs is Correct (without any errors)? (d) struct s{ int i; struct s *p; }; int main(){ struct s var 1, var 2; var1.i = 100; var2.i = 200; var1.p = &var2; var2.p = &var1; printf(" \n%d %d", var1.p->var2.p->i); return 0; }
d
What is the error in the below program? int main(){ FILE *fp, *ft; fp = fopen("FILE.txt", "r"); fp = fopen("Data.txt", "r"); fclose(fp, ft); return 0; }
fclose(fp, ft);
This function reads character by character
fgetc( )
This function reads a chunk of character based on the given size of until '\n'
fgets()
Similar to scanf where we can provide data type format to read from the file
fscanf( )
Consider the content of a well structured file that contains the temperature of different states. The first line contains the number of states. 5 FL 90 NY 50 MD 75 NC 80 SC 82 Which of the following is the most suitable function to read and load data from such well structured file?
fscanf()
Consider a file "data.txt" contains the following text: "This is a test data containing some text in the file" You have written the following lines of code: FILE *fp = fopen("data.txt", "r"); char ch[100]; fscanf(fp, "%s", ch ); fscanf(fp, "%s", ch ); What would be the value of ch string after executing the above piece of code?
is
What does the code *( *( n + i ) + j ) ) refer to in the below program? int main(){ int n[ 3][ 3 ] = { 2, 4, 3, 6, 8, 5, 3, 5, 1 }; int i, j; for( i = 0; i <= 2; i ++) for( j = 0; j <= 2; j++) printf(" \n%d %d %d", n[ i ][ j ], *(*( n + i ) + j)); return 0; }
n[ i ][ j ]
What would the value in A[1] after running the program? void encrypt( char A[ ][ 20 ], int cypher, int size){ for ( int i = 0; i < size; i++) for (int j = 0; A[ i ][ j ] != '\0'; j ++) A[ i ][ j ] = A [ i ][ j ] + cypher; } int main(void){ char arr[ 4 ][ 20 ] = { "ford", "nissan", "accura", "audi" }; encrypt(arr, 2, 5); for(int i = 0; i < 4; i ++){ printf(" %s\n", arr[ i ]); } return 0; }
pkuucp
If you access the member of the structure using a structure _______, then we use (->).
pointer
Consider there are two strings: char S1[6] = "cat", S2[10] = "red"; What would be the value of S2 after the following operation? strcat(S1, S2);
red
Consider there are two strings: char S1[6] = "cat", S2[10]; Which of the following will copy string S1 to S2?
strcpy( S2, S1 );
Consider a structure student. Which of the following is a correct way to receive a reference of a structure as a function parameter?
void func(struct student *record);
1.) The output of first printf is ______ 2.) and the output of second printf is _______ struct Point{ int x; int y; }; void add2(struct Point *p, int i , int j){ p->x += i; p->y += j; } int main(){ struct Point p1 = {0, 0}; add1( p1, 1, 1); printf("%d %d\n", p1.x, p1.y); add2(&p1, 1, 1); printf("%d %d", p1.x, p1.y); return 0; }
1.) 0 0 2.) 1 1
1.) The output from the first printf is ____________ 2.) The output from the second printf is __________ 3.) The output from the third printf is main(){ char c[ ] = "Intro to programming"; printf(" \n%s", &s[ 2 ]); printf(" \n%s", s ); printf(" \n%c", s[ 2 ]); }
1.) tro to programming with C! 2.) Intro to programming with C! 3.) t
Which of the following is the correct declaration (without any error)?
char x[100] = "A";
What would be the output from the second printf? If the number is a floating point number, write up to two decimal places. int main(){ float a = 13.5; float *b, *c; b = &a; //suppose address of a is 1006 c = b; printf(" \n%u %u %u", &a, b, c); printf(" \n%f %f %f %f", a, *(&a), *&a, *b, *c); return 0; }
13.50 13.50 13.50 13.50 13.50 (five 13.50's)
What would be the Output of the Following program? int main(){ int i = 4, j = 2; junk = (&i, j); printf("\n%d %d", i, j); return 0; } void junk(int *i, int j){ *i = *i * *i; j = j * j; }
16 2
What would be the Output of the Following program? int main(){ int i = 5, j = 2; junk = (&i, &j); printf("\n%d %d", i, j); return 0; } void junk(int *i, int *j){ *i = *i * *i; *j = *j * *j; }
25 4
Consider the following string: char S1[6] = "cat"; What is the value of strlen(S1)?
3
What is the output of the following program? int main(){ int a[ 5 ] = { 5, 1, 15, 20, 25 }; int i, j, k = 1, m; i = ++a[ 1 ]; j = a[ 1 ]++; m = a[i++]; printf(" \n%d %d %d", i, j, m ); return 0; }
3 2 15
Which of the below statements is false? 1.If a file is opened for reading, it is necessary that the file must exist. 2. If a file opened for writing already exists, its contents would be overwritten. 3. Use of fgets() function to read file also keeps the '\n' in the string. (read about fgets function in the file I/O lecture note) 4. If you open a file in write mode, your program will create a file in c drive of windows by default.
4
What would be the Output of the Following program? int main(){ int i = 5, j = 2; junk = (i, j); printf("\n%d %d", i, j); return 0; } void junk(int i, int j){ i = i * i; j = j * j; }
5 2
The array char name[10] can consist of a maximum of ______ characters to become a string.//last one is reserved for '\0'.
9
If you open the file using the command FILE*t; t=fopen ( "data.txt", "r"); where should the data.txt be stored. A. same folder where the program is located B. C:drive C. None of the above.
A
What is the output of the following program? (use space instead of next line) main(){ char c[ 2 ] = "A"; printf(" \n%c", c[ 0 ]); printf(" \n%s", c ); }
A A
Which of the following ways is the correct way to declare a variable of type Book? typedef struct Book{ char title[50]; int price; }Book;
Book b1;
Using big-oh notation, give the best-case runtime for inserting a new element into an AVL tree with n nodes
O(log n)
On opening a file for reading which of the following activities are performed: a) The disk is searched for existence of the file b)The file is brought into memory. c) A pointer is set up which points to the first character in the file. d)All of the above.
D, all the above
What is the output of the below program? int main(){ char s [ ] = "Da gaad feel gaad!" char x = 'o'; char t[ 25 ]; strcpy( t, s); int i = 0; while( s [ i ] != ' \0 ' ) { if( t [ i ] == ' a ' ) *(s + i) = x ; i ++; } printf(" \n%s", s); return 0; }
Do good feel good!
What is the error (if any) in this program? int main(){ int i = 35, *z; z = function( &i); printf(" \n%d", z); return 0; } int* function(int *m){ return( m + 2 ); }
Might result in Segmentation fault as m is not pointing an array and m+2 is trying to access illegal memory
Using big-oh notation, give the worst-case runtime for inserting a new element into an AVL tree with n nodes:
O(log n)
If you access the member of the structure using structure ________,then we use dot (.).
variable