Question 1
Question
Which Code sample will eventually cause the computer to run out of memory ?
Answer
-
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); } ```
Question 2
Question
What will this code print on the screen?
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");
}
Answer
-
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
-
Northing is printed on Screen
Question 3
Question
What is the name for calling a function inside the same function?
Answer
-
recursion
-
subfunction
-
inner call
-
infinite loop
Question 4
Question
What does the declaration of variable c2 demonstrate?
main(){
char c1 ='a';
char c2 = c1+10;
}
Answer
-
character arithmetic
-
undefined assignment
-
type conversion
-
invalid declaration
Question 5
Question
A pointer to void named vptr, has been set to point to a floating point variable named g. What is the valid way to dereference vptr to assign its pointed value to a float variable named f later in this program?
float g;
void *vptr=&g;
Answer
-
f = _(float _)vptr;
-
f = (float *)vptr;
-
f = *(float *)vptr;
-
f = *(float)vptr;
Question 6
Question
What is this declaration an example of?
struct s {
int i;
struct s *s1;
struct s *s2;
};
Answer
-
a node
-
a linked list
-
a stack
-
a binary tree
Question 7
Question
A C header file is a file with extension .h that contains function declarations and macro definitons to be shared between several source files. 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?
Answer
-
The preprocessor will try to locate the fileA in same directory as the source file, and the fileB in a predetermined directory path.
-
The preprocessor will try to locate the fileA in the fixed system directory. It will try to locate fileB in the directory path designated by the -l option added to the command line while compiling the source code.
-
The file using fileA syntax must be system files, of unlimited number. fileB must be a user file at a maximun of one per source file.
-
The preprocessor will try to locate the 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.
Question 8
Question
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?
Answer
-
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--; } ```
-
c int i; for (i= 10; i>0; i--){ printf("%d\n", i); }// end of loop ```
Question 9
Question
What is not one of the reserved words in standard C?
Answer
-
volatile
-
typeof
-
register
-
typedef
Question 10
Question
What does the program shown below return?
int main(){
int a=1, b=2, c=3, d=4;
int x = a;
if (a>b)
if (b<c) x=b;
else x=c;
return(x);
}
Question 11
Question
Using the Union declaration below, how many bytes of memory space will the data of this type occupy?
union Cars {
char make[20];
char model[30];
short year;
} car;
Question 12
Question
In this code sample, what is not a problem for C compiler?
main(){
constant int PI = 3.14;
printf("%f\n", pi);
}
Answer
-
The value of PI needs to be set to 3.141593, not 3.14
-
The declaration of PI needs to say const, not constant.
-
The data type of PI needs to be float not int.
-
The printf statement needs to use PI, not pi.
Question 13
Question
Which is the smallest program to compile and run without errors?
Answer
-
main()
-
int main() {return 0;}
-
main() { }
-
main() { ; }
Question 14
Question
What is optional in a function declaration?
Answer
-
data type of parameters
-
return type of function
-
parameter names
-
number of parameters
Question 15
Question
C treats all devices, such as the display and the keyboard, as files. Which files opens automatically when a program executes?
Answer
-
stdout
-
stdio.h
-
default.h
-
string.h
Question 16
Question
In which segment does dynamic memory allocation takes place?
Answer
-
BSS Segment
-
stack
-
heap
-
data segment
Question 17
Question
Which of the following do you use to deallocate memory?
Answer
-
dalloc()
-
dealloc()
-
release()
-
free()
Question 18
Question
In C language what are the basic building blocks that are constructed together to write a program?
Answer
-
keywords
-
identifiers
-
tokens
-
functions
Question 19
Question
When is memory for a variable allocated?
Answer
-
during the assigment of the variable
-
during the initialization of the variable
-
during the declaration of the variable
-
during the definition of the variable
Question 20
Question
By default c uses the call by value method to pass arguments to functions. How can you invoke the call by reference method?
Answer
-
by using pointers
-
by declaring functions separately from defining them
-
by using recursive functions
-
by using global variables
Question 21
Question
A union allows you to store different ___ in the same ___.
Question 22
Question
What is the output of this program?
main() {
char c1='a' , c2='A';
int i=c2-c1;
printf("%d", i);
}
Question 23
Question
What is the difference between scanf() and sscanf() functions?
Answer
-
The scanf() function reads data formatted as a string; The sscanf() function reads string input from the screen.
-
The scanf() function reads formatted data from the keyboard; The sscanf() function reads formatted input from a string.
-
The scanf() function reads string data from the keyboard; The sscanf() function reads string data from a string.
-
The scanf() function reads formatted data from a file; The sscanf() function reads input from a selected string
Question 24
Question
What is not a valid command with this declaration?
char *string[20] = { "one", "two", "three"};
Answer
-
printf("%c", string[1][2]);
-
printf("%s", string[1][2]);
-
printf("%s", string[1]);
-
printf(string[1]);
Question 25
Question
What is the expression player->name equivalent to?
Answer
-
player.name
-
(*player).name
-
*player.name
-
player.*name
Question 26
Question
Which program will compile and run without errors?
Answer
-
c main() { for(i=0; i<10; i++) ; } ```
-
c main() { int i=0; for(; i<10; i++) ; } ```
-
c main() { int i; for(i=0; i<j; i++) ; } ```
-
c main() { int i; for (i= 10; i<10; i++) } ```
Question 27
Question
What does this function call return?
1 main() { float x = f1(10, 5); }
2 float f1(int a, int b) { return (a/b); }
Answer
-
2
-
2.000000
-
a runtime error
-
a compiler error
Question 28
Question
What does this program create?
#include <stdio.h>
int main() {
int *p = NULL;
return 0;
}
Answer
-
a runtime error
-
a NULL pointer
-
a compile error
-
a void pointer
Question 29
Question
What is an alternative way to write the expression (*x).y?
Answer
-
There is no equivalent.
-
x->y
-
*x->y
-
y->x
Question 30
Question
Compile time errors are static errors that can be found where in the code?
Answer
-
in declarations and definitions
-
in functions and expressions
-
in syntax and semantics
-
in objects and statements
Question 31
Question
File input and output (I/O) in C is heavily based on the way it is done ___?
Answer
-
in Unix
-
in C++
-
in C#
-
in DOS
Question 32
Question
What does the strcmp(str1, str2); function return?
Answer
-
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