Amity B.SC IT 1 Sem Solve Assignment For Programming & Problem Solving through C Language Sem 1
Section
|
|||
|
|||
|
|||
|
|||
|
Section B
Case Detail :
QUESTIONS
|
|
|
Assignment-C
Multiple choice questions in C
Question No. 1
Code:
int z,x=5,y=-10,a=4,b=2; z = x++ - --y * b / a;
What number will z in the sample code above contain?
- 5
- 6
- 10
- 11
Question No. 2 Marks - 10
With every use of a memory allocation function, what function should be used to release allocated memory which is no longer needed?
Options
- unalloc()
- dropmem()
- dealloc()
- free()
Question No. 3
Code:
void *ptr;
myStruct myArray[10];
ptr = myArray;
Which of the following is the correct way to increment the variable "ptr"?
Options
- ptr = ptr + sizeof(myStruct);
- ++(int*)ptr;
- ptr = ptr + sizeof(myArray);
- increment(ptr);
Question No. 4
Code:
char* myFunc (char *ptr)
{
ptr += 3; return (ptr);
}
int main()
{
char *x, *y; x = "HELLO";
y = myFunc (x);
printf ("y = %s \n", y); return 0;
}
Options
- y = HELLO
- y = ELLO
- y = LLO
- y = LO
Question No. 5 Marks - 10
struct node *nPtr, *sPtr; /* pointers for a linked list. */ for (nPtr=sPtr; nPtr; nPtr=nPtr->next)
{
free(nPtr);
}
The sample code above releases memory from a linked list. Which of the choices below accurately describes how it will work?
Options
- It will work correctly since the for loop covers the entire list.
- It may fail since each node "nPtr" is freed before its next address can be accessed.
- In the for loop, the assignment "nPtr=nPtr->next" should be changed to "nPtr=nPtr.next".
- This is invalid syntax for freeing memory.
Question No. 6 Marks - 10
What function will read a specified number of elements from a file?
Options
- fileread()
- getline()
- readfile()
- fread()
Question No. 7 Marks - 10
"My salary was increased by 15%!" Select the statement which will EXACTLY reproduce the line of text above.
Options
- printf("\"My salary was increased by 15/%\!\"\n");
- printf("My salary was increased by 15%!\n");
- printf("My salary was increased by 15'%'!\n");
- printf("\"My salary was increased by 15%%!\"\n");
Question No. 8 Marks - 10
What is a difference between a declaration and a definition of a variable?
Options
- Both can occur multiple times, but a declaration must occur first.
- There is no difference between them.
- A definition occurs once, but a declaration may occur many times.
- A declaration occurs once, but a definition may occur many times.
Question No. 9 Marks - 10
int testarray[3][2][2] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12}; What value does testarray[2][1][0] in the sample code above contain?
Options
- 3
- 5
- 11
- 9
Question No. 10 Marks - 10
Code:
int a=10,b; b=a++ + ++a;
printf("%d,%d,%d,%d",b,a++,a,++a);
What will be the output when following code is executed
Options
- 12,10,11,13
- 22,13,13,13
- 22,10,11,13
- 22,11,11,11
Question No. 11 Marks - 10
Code:
int x[] = { 1, 4, 8, 5, 1, 4 };
int *ptr, y;
ptr = x + 4;
y = ptr - x;
What does y in the sample code above equal?
Options
- -3
- 0
- 4
- 4 + sizeof( int )
Question No. 12 Marks - 10
Code:
void myFunc (int x)
{
if (x > 0) myFunc(--x);
printf("%d, ", x);
}
int main()
{
myFunc(5); return 0;
}
What will the above sample code produce when executed?
Options
- 1, 2, 3, 4, 5, 5,
- 4, 3, 2, 1, 0, 0,
- 5, 4, 3, 2, 1, 0,
- 0, 0, 1, 2, 3, 4,
Question No. 13 Marks - 10
What does the operation shown above produce?
Options
- 1
- 6
- 8
- 14
Question No. 14 Marks - 10
#define MAX_NUM 15 Referring to the sample above, what is MAX_NUM?
Options
- MAX_NUM is an integer variable.
- MAX_NUM is a linker constant.
- MAX_NUM is a precompiler constant.
- MAX_NUM is a preprocessor macro.
Question No. 15 Marks - 10
Which one of the following will turn off buffering for stdout?
Options
- setbuf( stdout, FALSE );
- setvbuf( stdout, NULL );
- setbuf( stdout, NULL );
- setvbuf( stdout, _IONBF );
Question No. 16 Marks - 10
What is a proper method of opening a file for writing as binary file?
Options
- FILE *f = fwrite( "test.bin", "b" );
- FILE *f = fopenb( "test.bin", "w" );
- FILE *f = fopen( "test.bin", "wb" );
- FILE *f = fwriteb( "test.bin" );
Question No. 17 Marks - 10
Which one of the following functions is the correct choice for moving blocks of binary data that are of arbitrary size and position in memory?
Options
- memcpy()
- memset()
- strncpy()
- memmove()
Question No. 18 Marks - 10
int x = 2 * 3 + 4 * 5; What value will x contain in the sample code above?
Options
- 22
- 26
- 46
- 50
Question No. 19 Marks - 10
Code:
void * array_dup (a, number, size) const void * a;
size_t number; size_t size;
{
void * clone; size_t bytes; assert(a != NULL);
bytes = number * size; clone = alloca(bytes); if (!clone)
return clone;
memcpy(clone, a, bytes);
return clone;
}
The function array_dup(), defined above, contains an error. Which one of the following correctly analyzes it?
Options
- If the arguments to memcpy() refer to overlapping regions, the destination buffer will be subject to memory corruption.
- array_dup() declares its first parameter to be a pointer, when the actual argument will be an array.
- The memory obtained from alloca() is not valid in the context of the caller. Moreover, alloca() is nonstandard
- The definition of array_dup() is unusual. Functions cannot be defined using this syntax.
Question No. 20 Marks - 10
int var1; If a variable has been declared with file scope, as above, can it safely be accessed globally from another file?
Options
- Yes; it can be referenced through the register specifier.
- No; it would have to have been initially declared as a static variable.
- No; it would need to have been initially declared using the global keyword.
- Yes; it can be referenced through the publish specifier.
Question No. 21 Marks - 10
time_t t; Which one of the following statements will properly initialize the variable t with the current time from the sample above?
Options
- t = clock();
- time( &t );
- t = ctime();
- t = localtime();
Question No. 22 Marks - 10
Which one of the following provides conceptual support for function calls?
Options
- The system stack
- The data segment
- The processor's registers
- The text segment
Question No. 23 Marks - 10
C is which kind of language?
Options
- Machine
- Procedural
- Assembly
- Object-oriented
Question No. 24 Marks - 10
Code:
int i,j; int ctr = 0;
int myArray[2][3]; for (i=0; i<3; i++)
for (j=0; j<2; j++)
{
myArray[j][i] = ctr;
++ctr;
}
What is the value of myArray[1][2]; in the sample code above?
Options
- 5 00,10,01,11,12
- 1
- 2
- 3
Question No. 25 Marks - 10
Code:
int x = 0;
for (x=1; x<4; x++);
printf("x=%d\n", x);
What will be printed when the sample code above is executed?
Options
- x=0
- x=1
- x=3
- x=4
Question No. 26 Marks - 10
Code:
int x = 3; if( x == 2 );
x = 0;
if( x == 3 )
x++;
else x += 2;
What value will x contain when the sample code above is executed?
Options
- 1
- 2
- 3
- 4
Question No. 27 Marks - 10
Code:
char *ptr;
char myString[ ] = "abcdefg";
ptr = myString; ptr += 5;
What string does ptr point to in the sample code above?
Options
- fg /*because string*/
- efg
- defg
- cdefg
Question No. 28 Marks - 10
Code:
double x = -3.5, y = 3.5;
printf( "%.0f : %.0f\n", ceil( x ), ceil( y ) );
printf( "%.0f : %.0f\n", floor( x ), floor( y ) );
What will the code above print when executed? ceil =>rounds up 3.2=4 floor =>rounds down 3.2=3
- -3 : 4
-4 : 3
- -4 : 4
-3 : 3
- -4 : 3
-4 : 3
- -4 : 3
-3 : 4
Question No. 29 Marks - 10
Which one of the following will declare a pointer to an integer at address 0x200 in memory?
Options
- int *x; *x = 0x200;
- int *x = &0x200;
- int *x = *0x200;
- int *x = 0x200; Choice 5
Question No. 30 Marks - 10
Code:
int x = 5;
int y = 2;
char op = '*';
switch (op)
{
default : x += 1;
case '+' : x += y; /*It will go to all the cases*/
case '-' : x -= y;
}
After the sample code above has been executed, what value will the variable x contain?
Options
- 4
- 5
- 6
- 7
Question No. 31 Marks - 10
Code:
x = 3, counter = 0; while ((x-1))
{
++counter; x--;
}
Referring to the sample code above, what value will the variable counter have when completed?
Options
- 0
- 1
- 2
- 3
Question No. 32 Marks - 10
char ** array [12][12][12]; Consider array, defined above. Which one of the following definitions and initializations of p is valid?
Options
- char ** (* p) [12][12] = array;
- char ***** p = array;
- char * (* p) [12][12][12] = array;
- const char ** p [12][12][12] = array;
Question No. 33 Marks - 10
void (*signal(int sig, void (*handler) (int))) (int); Which one of the following definitions of sighandler_t allows the above declaration to be rewritten as follows: sighandler_t signal (int sig, sighandler_t handler);
Options
- typedef void (*sighandler_t) (int);
- typedef sighandler_t void (*) (int);
- typedef void *sighandler_t (int);
- #define sighandler_t(x) void (*x) (int)
Question No. 34 Marks - 10
All of the following choices represent syntactically correct function definitions. Which one of the following represents a semantically legal function definition in Standard C?
Options
- Code:
int count_digits (const char * buf) {
assert(buf != NULL);
int cnt = 0, i;
for (i = 0; buf[i] != '\0'; i++)
if (isdigit(buf[i]))
cnt++;
return cnt;
}
- Code:
int count_digits (const char * buf) {
int cnt = 0;
assert(buf != NULL);
for (int i = 0; buf[i] != '\0'; i++)
if (isdigit(buf[i]))
cnt++;
return cnt;
}
- Code:
int count_digits (const char * buf) {
assert(buf != NULL);
for (i = 0; buf[i] != '\0'; i++)
if (isdigit(buf[i]))
cnt++;
return cnt;
}
- Code:
int count_digits (const char * buf) {
assert(buf != NULL);
int cnt = 0;
for (int i = 0; buf[i] != '\0'; i++)
if (isdigit(buf[i]))
cnt++;
return cnt;
}
Question No. 35 Marks - 10
struct customer *ptr = malloc( sizeof( struct customer ) ); Given the sample allocation for the pointer "ptr" found above, which one of the following statements is used to reallocate ptr to be an array of 10 elements?
Options
- ptr = realloc( ptr, 10 * sizeof( struct customer));
- realloc( ptr, 9 * sizeof( struct customer ) );
- ptr += malloc( 9 * sizeof( struct customer ) );
- ptr = realloc( ptr, 9 * sizeof( struct customer ) );
Question No. 36 Marks - 10
Which one of the following is a true statement about pointers?
Options
- Pointer arithmetic is permitted on pointers of any type.
- Pointers may be used to simulate call-by-reference.
- A pointer of type void * can be used to directly examine or modify an object of any type.
- Standard C mandates a minimum of four levels of indirection accessible through a pointer.
Question No. 37 Marks - 10
Which one of the following functions returns the string representation from a pointer to a time_t value?
Options
- localtime
- gmtime
- ctime
- strtime
Question No. 38 Marks - 10
Code:
short testarray[4][3] = { {1}, {2, 3}, {4, 5, 6} }; printf( "%d\n", sizeof( testarray ) );
Assuming a short is two bytes long, what will be printed by the above code?
Options
- It will not compile because not enough initializers are given.
- 6
- 24
- 7
Question No. 39 Marks - 10
char buf [] = "Hello world!"; char * buf = "Hello world!"; In terms of code generation, how do the two definitions of buf, both presented above, differ?
Options
- The first definition certainly allows the contents of buf to be safely modified at runtime; the second definition does not.
- The first definition is not suitable for usage as an argument to a function call; the second definition is.
- The first definition is not legal because it does not indicate the size of the array to be allocated; the second definition is legal.
- They do not differ -- they are functionally equivalent.
Question No. 40 Marks - 10
In a C expression, how is a logical AND represented?
Options
- @@
- ||
- .AND.
- &&