# Which Code sample will eventually cause the computer to run out of memory?
- [x] :
```c
while(1)
{
char *smallString = (char *) malloc(10);
}
```
- [ ] :
```c
long long number = 1;
while(1)
number *= 2;
```
- [ ] :
```c
while(1)
{
char hugeString[1000000L];
memset(hugeString, 0, 1000000L);
}
```
- [ ] :
```c
while(1)
{
long *bigArray = (long *) malloc(sizeof(long) * 1000);
memset(bigArray, 1000000, 1000);
free(bigArray);
}
```
# What will this code print on the screen?
```c
int f1 (int a, int b)
{
if (a > b)
{
printf("A is greater than B\n");
return 1;
}
else
{
printf("B is greater than A");
return 0;
}
}
main()
{
if (f1(20,10) || f1(10,20))
printf("C is fun!\n");
}
```
- [x] :
```
A is greater then B
C is fun!
```
- [ ] :
```
A is greater then B
B is greater then A
C is fun!
```
- [ ] :
```
A is greater then B
B is greater then A
```
- [ ] Nothing is printed on Screen
# What is the name for calling a function inside the same function?
- [x] recursion
- [ ] subfunction
- [ ] inner call
- [ ] infinite loop
# What does the declaration of variable c2 demonstrate?
```c
main(){
char c1 ='a';
char c2 = c1+10;
}
```
- [x] character arithmetic
- [ ] undefined assignment
- [ ] type conversion
- [ ] invalid declaration
# What is this declaration an example of?
```c
struct s {
int i;
struct s *s1;
struct s *s2;
};
```
- [x] a node
- [ ] a linked list
- [ ] a stack
- [ ] a binary tree
# Header files are listed using the preprocessing directive #include, and can have one of the following formats: #include <fileA> or #include "fileB". What is the difference between these two formats?
- [ ] The preprocessor will try to locate fileA in same directory as the source file, and the fileB in a predetermined directory path.
- [ ] The preprocessor will try to locate fileA in the fixed system directory. It will try to locate fileB in the directory path designated by the -I option added to the command line while compiling the source code.
- [ ] The file using the fileA syntax must be system files, of unlimited number; fileB must be a user file at a maximun of one per source file.
- [x] The preprocessor will try to locate fileA in a predetermined directory path. It will try to locate fileB in the same directory as the source file along with a custom directory path.
# Using a for loop, how could you write a C code to count down from 10 to 1 and display each number on its own line?
- [ ] :
```c
for (int i = 0; i>=0, i--){
printf("%d\n", i);
}//end of loop
```
- [ ] :
```c
int i;
for (i=1; i<=10; i++){
printf("%d", i);
}
```
- [ ] :
```c
int i = 10;
while (i>0){
printf("%d\n", i);
i--;
}
```
- [x] :
```c
int i;
for (i= 10; i>0; i--){
printf("%d\n", i);
}// end of loop
```
# What is not one of the reserved words in standard C?
- [ ] volatile
- [x] typeof
- [ ] register
- [ ] typedef
[Reference](https://www.ibm.com/docs/en/adfz/developer-for-zos/14.2.0?topic=programs-c-reserved-keywords)
# What does the program shown below return?
```c
int main(){
int a=1, b=2, c=3, d=4;
int x = a;
if (a>b)
if (bname equivalent to?
- [ ] `player.name`
- [x] `(*player).name`
- [ ] `*player.name`
- [ ] `player.*name`
# Which program will compile and run without errors?
- [ ] :
```c
main() {
for(i=0; i<10; i++) ;
}
```
- [x] :
```c
main() {
int i=0;
for(; i<10; i++) ;
}
```
- [ ] :
```c
main() {
int i;
for(i=0; i
int main() {
int *p = NULL;
return 0;
}
```
- [ ] a runtime error
- [x] a NULL pointer
- [ ] a compile error
- [ ] a void pointer
# What is an alternative way to write the expression (\*x).y?
- [ ] There is no equivalent.
- [x] x->y
- [ ] \*x->y
- [ ] y->x
# Compile time errors are static errors that can be found where in the code?
- [x] in declarations and definitions
- [ ] in functions and expressions
- [ ] in syntax and semantics
- [ ] in objects and statements
# File input and output (I/O) in C is heavily based on the way it is done `___`?
- [x] in Unix
- [ ] in C++
- [ ] in C#
- [ ] in DOS
# What does the strcmp(str1, str2); function return?
- [x] 0 if str1 and str2 are the same, a negative number if str1 is less than str2, a positive number if str1 is greater than str2
- [ ] true (1) if str1 and str2 are the same, false (0) if str1 and str2 are not the same
- [ ] true (1) if str1 and str2 are the same, NULL if str1 and str2 are not the same
- [ ] 0 if str1 and str2 are the same, a negative number if str2 is less than str1, a positive number if str2 is greater than str1
# What is the output of this program?
```c
int a=10, b=20;
int f1(a) { return(a*b); }
main() {
printf("%d", f1(5));
}
```
- [x] 100
- [ ] 200
- [ ] 5
- [ ] 50
# Which is _not_ a correct way to declare a string variable?
- [ ] `char *string = "Hello World";`
- [x] `char string = "Hello World";`
- [ ] `char string[20] = {'H', 'e', 'l', 'l', 'o', ' ', 'W', 'o', 'r', 'l', 'd'};`
- [ ] `char string[] = "Hello World";`
# Which choice is an include guard for the header file mylib.h?
- [ ] :
```c
#ifdef MYLIB_H
#undef MYLIB_H
// mylib.h content
#endif /* MYLIB_H */
```
- [x] :
```c
#ifndef MYLIB_H
#define MYLIB_H
// mylib.h content
#endif /* MYLIB_H */
```
- [ ] :
```c
#define MYLIB_H
#include "mylib.h"
#undef MYLIB_H
```
- [ ] :
```c
#ifdef MYLIB_H
#define MYLIB_H
// mylib.h content
#endif /* MYLIB_H */
```
# How many times does the code inside the while loop get executed in this program?
```c
main(){
int x=1;
while(x++<100){
x*=x;
if(x<10) continue;
if(x>50) break;
}
}
```
- [ ] 100
- [x] 3
- [ ] 5
- [ ] 50
# File input and output (I/O) in C is done through what?
- [ ] syntax-driven components
- [ ] native interfaces
- [ ] system objects
- [x] function calls
# Directives are translated by the?
- [x] Pre-processor
- [ ] Compiler
- [ ] Linker
- [ ] Editor
# The main loop structures in C programming are the for loop, the while loop, and which other loop?
- [x] do...while
- [ ] for...in
- [ ] repeat...until
- [ ] do...until
# By default, C Functions are what type of functions?
- [ ] global
- [ ] static
- [x] library
- [ ] system
# You have written a function that you want to include as a member of structure a. How is such as structure member defiened?
- [x] :
```c
struct a {
void *f1;
};
```
- [ ] :
```c
struct a {
void (*f1)();
};
```
- [ ] :
```c
struct a {
*(void *f1)();
};
```
- [ ] :
```c
struct a {
void *f1();
};
```
# A Stack data structure allows all data operations at one end only, making it what kind of an implementation?
- [ ] FIFO
- [x] LIFO
- [ ] LILO
- [ ] LOLI
# What does this program display?
```c
main(){
char *p = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
int i;
for (i=0;i<5;i++) *p++; *p++;
printf("%c",*p++);
}
```
- [ ] K
- [ ] M
- [ ] H
- [x] G
# Describe the relationship between lvalue and rvalue.
- [ ] An lvalue may appear only on the left-hand side of an assignment; an rvalue may appear only on the right-hand side.
- [ ] An lvalue may appear only on the left-hand side of an assignment; an rvalue may appear on either the left-hand or right-hand side.
- [ ] An lvalue and an rvalue may appear on either left-hand or right-hand side of an assignment.
- [x] An lvalue may appear on the left-hand or right-hand side of an assignment; an rvalue may appear only on the right-hand side.
# Which operator is used to access the address of a variable?
- [ ] `%`
- [ ] `**`
- [ ] `*`
- [x] `&`
# Which add function properly returns the updated value of result?
- [x] :
```c
void add (int a, int b, int *result)
{
*result = a+b;
}
main()
{
int a = 10;
int b = 20;
int result = 0;
add(a,b,&result);
}
```
- [ ] :
```c
void add (int a, int b, int result)
{
result = a+b;
}
main()
{
int a = 10;
int b = 20;
int result = 0;
add(a,b,result);
}
```
- [ ] :
```c
void add (int a, int b, int *result)
{
result = a+b;
}
main()
{
int a = 10;
int b = 20;
int result = 0;
add(a,b,result);
}
```
- [ ] :
```c
void add (int *a, int *b, int *result)
{
result = a+b;
}
main()
{
int a = 10;
int b = 20;
int result = 0;
add(*a,*b,*result);
}
```
# Consider the number of the Fibonacci series below 100: 0,1,1,2,3,5,8,13,21,34,55,89. Which piece of code outputs the sequence?
- [ ] :
```c
void fibonacci(int a, int b)
{
int c = a+b;
if(a>100)
return;
printf("%d", a);
fibonacci(a,b);
}
int main()
{
fibonacci(0,1);
}
```
- [ ] :
```c
void fibonacci(int a, int b)
{
int c = a+b;
if(a>100)
return;
printf("%d", b);
fibonacci(a,c);
}
int main()
{
fibonacci(0,1);
}
```
- [x] :
```c
void fibonacci(int a, int b)
{
int c = a+b;
if(a>100)
return;
printf("%d", a);
fibonacci(b,c);
}
int main()
{
fibonacci(0,1);
}
```
- [ ] :
```c
void fibonacci(int a, int b)
{
int c = a+b;
if(a>100)
return;
printf("%d", c);
fibonacci(b,c);
}
int main()
{
fibonacci(0,1);
}
```
# Which is _not_ a storage class specifier?
- [x] `intern`
- [ ] `extern`
- [ ] `register`
- [ ] `static`
[Reference](https://en.cppreference.com/w/cpp/language/storage_duration)
# Which line of code, after execution, results in `i` having the value of 1?
- [ ] `for(i=1; i<=1; i++);`
- [ ] `for(i=1; i=10; i++);`
- [x] `for(i=1; i==10; i++);`
- [ ] `for(i=10; i>=1; i--);`
# What is the value of variable c at the end of this program?
```
1 main() {
2 int a, b, c;
3 a=10; b=50;
4 c=a * b % a;
5 }
```
- [ ] 50
- [ ] 5
- [x] 0
- [ ] 500
# What is _not_ one of the basic data types in C
- [ ] long double
- [ ] unsigned char
- [x] array
- [ ] float
# What is the member access operator for a structure?
- [ ] ,
- [ ] []
- [x] .
- [ ] :
# What standard data type provides the smallest storage size and can be used in computations?
- [x] char
- [ ] float
- [ ] int
- [ ] short
# what does the ctype tolower() function do?
- [ ] It returns TRUE for lowercase letters of the alphabet.
- [ ] It ensures that text output uses only ASCII values (0 through 127).
- [ ] It returns FALSE for lowercase letters of the alphabet.
- [x] It converts an uppercase letter of the alphabet to lowercase.
# Void pointer _vptr_ is assigned the address of float variable _g_. What is a valid way to dereference _vptr_ to assign its pointed value to a float variable named _f_ later in the program?
```c
float g;
void *vptr=&g;
```
- [ ] `f=(float *)vptr;`
- [x] `f=*(float *)vptr;`
- [ ] `f=*(float)vptr;`
- [ ] `f=(float)*vptr;`
# The dynamic memory allocation functions are defined in which system header file ?
- [ ] stdio.h
- [x] stdlib.h
- [ ] limits.h
- [ ] stddef.h
# A function is a set of **\_**.
- [ ] declarations
- [x] statements
- [ ] variables
- [ ] objects
# How are static functions different from global functions?
- [ ] Static functions must be declared in advance of being defined.
- [ ] Static functions must be declared is a separate header file.
- [ ] Static functions always return the same value.
- [x] Static functions can be accessed only in the file where they are declared.
# Which code example creates the string "Hello Mars" in storage buffer `hello`.
- [ ] :
```c
char hello[25];
strcpy(hello, "Hello ");
strcpy(hello, "Mars");
```
- [x] :
```c
char hello[25];
char *p;
strcpy(hello, "Hello World");
p = hello;
p +=6;
strcpy(p, "Mars");
```
- [ ] :
```c
char *hello;
strcpy(hello, "Hello World");
hello+=6;
strcpy(hello, "Mars");
```
- [ ] :
```c
char hello[25];
strcpy(hello, "Hello World");
strcpy(*hello[6], "Mars");
```
# If you use the fopen() function with the "a" mode, what happens if the named file doesn't exist?
- [ ] The file is created and opened for reading.
- [x] The file is created and opened for writing.
- [ ] The fopen() function returns a NULL indicating that the operation has failed.
- [ ] The file is created and opened for both writing and reading
[Reference](https://www.tutorialspoint.com/c_standard_library/c_function_fopen.htm)
# What does this function return?
```c
int fl(int a, int b) { return(a>b?a:b); }
```
- [ ] compiler error
- [ ] the smaller value of the two passed parameters
- [ ] runtime error
- [x] the greater value of the two passed parameters