COP 3514 All Quizzes

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

Suppose the following declarations are in effect: int a[] = {5, 15, 34, 54, 14, 2, 52, 72}; int *p = &a[1], *q = &a[5]; What is the value of p - q?

-4

Which GCC option has to be used to compile the following code: #include <stdio.h> int main(void) { for(int i=0; i < 10; i++) { printf("Hello, world!\n"); } return 0; }

-std=c99

Which of the function definitions for a function isletter will correctly determine whether a character is an alphabetic letter and returns 1 for true and 0 for false? 1. int isletter(char ch) { if((ch >= 'a' && ch <= 'z')||(ch >= 'A' && ch <= 'Z')) return 1; else return 0; } 2. int isletter(char ch) { if((ch >= 'a' || ch <= 'z')&&(ch >= 'A' || ch <= 'Z)) return 1; else return 0; }

1

1) Can fscanf do everything that can be done with scanf? 2) Can fprintf do everything that can be done with printf?

1) YES and 2) YES

If a is a one-dimensional array and p is a pointer variable, and we perform the assignment p = a, which of the following expressions do not have mismatched types? p == &a[0] *p == *a p[0] == a[0] *p == a[0] p == a[0] *p == a &p == a *p == &a[0]

1,2,3,4

Which of the following are valid declarations of array of strings? char planets[][8] = {"Mercury", "Venus", "Earth"}; char planets[][] = {"Mercury", "Venus", "Earth"}; char *planets[] = {"Mercury", "Venus", "Earth"}; char planets[3][] = {"Mercury", "Venus", "Earth"}; char planets[] = {"Mercury", "Venus", "Earth"};

1,3

Which of the following are valid ways of accessing the value of the fourth element of an array a? a[3] a+3 *a+3 *(a+3) *&a[3] &a+3 &(a+3) (a+3)[0]

1,4,5,8

What would be the output of the below program? #include <stdio.h> main(){ int x[] = {100, 200, 300}; printf("%d", *x +1); }

101

How many times the function fib will be called when N is 5? #include <stdio.h> int fib(int n) { if (n <= 1) { return n; } return fib(n-1) + fib(n-2); } int main(void) { int N; scanf("%d", &N); printf("%d\n", fib(N)); return 0; }

15

Consider the following recursive function. int compute(int low, int high){ if (low == high) return 0; else return 1 + compute(low+1, high); } What does a call compute(3, 5) return?

2

Let f be the following function: int f(char *s, char *t) { char *p1, *p2; for(p1 = s, p2 = t; *p1 != '\0'&& *p2 != '\0'; p1++, p2++){ if (*p1 == *p2) break; } return p1 -s; } What is the return value of f("accd", "dacd")?

2

What would be the output of the below program? #include <stdio.h> main(){ int x[] = {100, 200, 300}; printf("%d", *(x +1)); }

200

How many integers will be allocated by the following instruction: int *p = malloc(100);

25

What is the output of the following code: #include <stdio.h> #define WEEKLY 7 #define BIWEEKLY WEEKLY+WEEKLY int main(void) { int days = 10*BIWEEKLY; printf("%d\n", days); return 0; }

77

Which operations can be performed on structures in C?

=

Memory Leak

A program that doesn't deallocate memory -- free(x)

Which of the words below is going to be "less than" all other words when compared through strcmp?

ATARI

Calloc

Allocates a block of memory and clears it.

Malloc

Allocates a block of memory but doesn'tinitialize it.

The function fopen return NULL when:

An error occurred while opening the file.

Dangling Pointer

Attempt to access freed memory/pointer

The following function returns a pointer to the last node that contains n as number; and it returns NULL if n doesn't appear in the list. Assume that the node structure is defined as struct node{ int number; char name[51]; struct node *next; } Which one of the following statements are correct for the missing statement in the function definition: struct node * find_last(struct node *list, int n){ struct node * p = NULL; sturct node *q = NULL; for(p = list; p !=NULL;p=p->next) //missing statements } A) if(p->number == n) break; return p; B) if(p->number == n) { q = p; break; } return q; C) if(p->number == n) q = p; return q;

C

What is the task carried out by the mystery function below: struct node *mistery(struct node *list, int n) { struct node *cur, *prev; for(cur=list, prev=NULL; cur != NULL && cur->value != n;prev = cur, cur = cur->next) /*nothing*/ ; if(cur == NULL) return list; /* n was not found */ if(prev == NULL) list = list->next; /* n is in the first node */ else prev->next = cur->next; /* n is in some other node */ free(cur); return list; }

Delete the first occurrence of the integer n from the linked list.

Heap

Dynamically allocated memory

When declaring an array a as a parameter of a function f, both *a and a[] can be used: void f(int *a); void f(int a[]); Regarding this declaration, the following statement is true:

There is not different between the two options.

Please choose the only correct observation regarding the piece of code below. switch (grade) { case 'A': case 'B': case 'C': case 'D': printf("Passing"); break; case 'F': printf("Failing"); break; default: printf("Illegal grade"); }

There is nothing wrong with this code.

When searching for an element in a linked list of person names, are there any advantages in using a sorted linked list over an unsorted linked list of names?

Yes. In a sorted linked list, we can stop the search early when the searched name is not in the list, which is not possible in an unsorted linked list.

When compiling the code below, what is the expected outcome? #include <stdio.h> int main(void) { int i=14; double x=4.5; printf("%d %lf\n", x, i); return 0; }

You might get a warning message.

In what order the expression below will be solved? a + b++ * - c / d

a + (((b++) * (- c)) / d)

#include <string.h> #define NAME_LEN 100 struct part{ int number; char name[NAME_LEN+1]; int on_hand; }; struct part build_part(int number, char *name, int on_hand) { struct part p; p.number = number; strcpy(p.name, name); p.on_hand = on_hand; return p; } int main() { struct part p1 = build_part(10,"Memory",4); }

a copy of the contents of p is assigned to p1.

Suppose you have just opened a file pFile on permit request records with the following data formats COP2510 84652123 pending COP3514 17680087 approved ...(more data) The first field in a row is the course id, the second is the student id, the third is the status of the request (pending or approved). What statement is correct for reading a row of data? Assume student_id is an int variable, course_id is a string variable, and status is a string variable.

fscanf(pFile, "%s %d %s", course_id, &student_id, status);

Suppose you have just opened the file test.txt and the following variables have been declared. char name[31]; int score; File test.txt consists of the following content: Mary 89 Mike 75 John 84 What statement will read the name and score in a line from the file?

fscanf(pFile, "%s %d", name, &score);

Which of the following correctly declares a dynamic array of integers of size n?

int *a; a = malloc(n* sizeof(int));

The function fgets():

is safer than the function gets() because you can specify the maximum number of chars to read.

Given a linked list of node declared as follows, which one of the following conditions checks if a linked list of nodes named list is an empty list? struct node { int number; struct node *next; };

list == NULL

Suppose that structure s and variable p are declared as follows: struct new_struct{ char a; int b[10]; }; struct new_struct s; Which one of the following statements will update the 3rd element of b in the structure s to 34?

s.b[2]=34

Given the following struct declaration and initialization, which condition will determine if s1 and s2 contain the same values for num and str? struct s{ int num; char str[31]; } struct s s1, s2; Which condition will determine if s1 and s2 contain the same values for num and str?

s1.num == s2.num &&strcmp(s1.str, s2.str)==0

Consider the following code: struct date{ int month; int day; int year; }; struct date d1; Which one of the following reads in d1's month?

scanf("%d", &d1.month);

Stack

Local Variables

How can we access the element of the i-th row and j-th column of a 2D array named MAT in C?

MAT[i][j]

What is the problem with following code? #include <stdio.h> #include <stdlib.h> int main(){ int *p = malloc(20* sizeof(int)); p = NULL; free(p); return 0; }

Memory leak

In the code below, how many times the block-statement within the loop will be executed when N is prime? #include <stdio.h> int main() { int is_prime=1, N; scanf("%d", &N); for(int i=2; i < N; i++) { if(N%i == 0) { is_prime=0; break; } } if(is_prime) { printf("%d is prime\n", N); } else { printf("%d is not prime\n", N); } return 0; }

N-2

What will be the output of the below program? #include <stdio.h> int main(){ char x[]="ProgramDesign", y[]="ProgramDesign"; if(x==y){ printf("Strings are Equal"); } }

No output

Which of the following options is a good way of reading an entire line of text from the input and storing it in an array of char named str?

None of these options work.

struct node *first = NULL;

Points to the first node in the list

Code

Program instructions

Realloc

Resizes a previously allocated block of memory.

What does the following program open "source.txt" to do?#include<stdio.h> int main () { FILE *fp; fp = fopen("source.txt", "r"); return 0; }

For Reading

What does the following program open "source.txt" to do?#include <stdio.h> int main () { FILE *fp; fp = fopen("source.txt", "w"); return 0; }

For writing and creating a new file "source.txt" if "source.txt" doesn't exist

Which one of the following should NOT be in a header file?

Function definition

Static Data

Global Variables

Which of the following describe the goal of code abstraction?

Hiding complex details from user. Creating simple, high-level user interfaces. Facilitating software maintenance.

In a well-designed program, modules should have two properties:

High cohesion and low coupling

If you had to implements a linked list that has to support thousands of insertions per day, but only a couple of searches in the same period, what would be the ideal place to add new elements to the list?

The beginning of the linked list.

The following program supposes to sum all entered int values that are greater than 5. It takes integer input and -1 indicates the end of input. It compiles without any error message, and it executes without error message, but nevertheless is wrong. What is wrong with the program? #include <stdio.h> int main() { int x, sum = 0; while (x != -1) { scanf("%d", &x); if (x > 5); sum = sum +x; } printf("The sum of values > 5 is %d\n", sum); }

The semicolon at the end of the if statement causes all entered values to be summed.

Why do we use the option '-Wall' when compiling C code with GCC?

To print warning messages about questionable constructions that might not necessarily be an error.

After some piece of memory allocated with malloc is no longer needed, the programmer must:

always release it using the function free.

Which one of the following would exchange the values of the variables x and y successfully? a) int tmp; tmp = x; x=y; y=x; b) int tmp; tmp = x; x=y; y=tmp; c) int tmp; tmp = y; x=y; y=tmp;

b

Which of the following defines a recursive function for calculating the factorial of a number? a) int fact(int n) { if (n==1) return 1; else return fact(n*(n-1)); } b) int fact(int n) { if (n==1) return 1; else return n*(n-1); } c) int fact(int n) { if (n==1) return 1; else return n*fact(n-1); }

c

If an user executes a program with the following command line: ./a.out 8 24 52 81 23 It is correct to say that the command-line arguments will have the following type in C code:

char *

Which of the following is a valid way of declaring a string str with initial value "word".

char str[] = {'w', 'o', 'r', 'd', '\0'};

Given a linked list of node declared as follows, which of the following conditions checks if p is pointing to the last node of the linked list named list. struct node { int number; struct node *next; };

p ->next == NULL

Suppose you want to read the file text.txt from the current working directory. Which one of the following statements correctly uses fopen function to open the file for reading? FILE* pFile;

pFile = fopen("test.txt", "r" );

What is performed by the following statement: fscanf("%[^,]", a);

reads a string containing any character other than ',' and does not consume the first ',' found.

Consider the following code, which is part of a program that maintains student records. struct record { int ID; char last_name[10]; char first_name[10]; int score; } ; ... /* This computes and returns the exam average for an array of student record */ double compute_average (struct record [] rec, int n) { int i; int total = 0; for(i = 0; i < n; i++) { statement /* compute total exam score */ } return (double) total/n; } Which of the following statements in place of statement will correctly calculate the total of student's score in the array?

total += rec[i].score;

What is the return type of malloc()?

void *

The function feof() can be used to detect:

when a reading operation reaches the end of a file.


Conjuntos de estudio relacionados

C952 Computer Architecture Chapter 4

View Set

Paraphrasing, Quoting, and Summarizing

View Set

Google Certification - Level 2 (Units 8-10)

View Set

Chapter 4: Activity-Based Costing SmartBook

View Set

Graph of Intracellular DNA mass vs Time

View Set

06-09 list creation from iterator

View Set

econ chpt 9 Saving, investment, and the financial system

View Set

Orpheus and Eurydice commonlit answers

View Set

BUSI - Ch. 12 (Distributing and Promoting Products and Services)

View Set