Programming & Problem Solving through C Language V1

Contact www.solvedcare.com for best and lowest cost solution or email solvedcare@gmail.com

Programming & Problem Solving through C Language V1
Assignment-A
1. Evaluate the expression 2+3*3-7+10-5/4+3 Using operator precedence.
2. What are the storage classes available in c language?
3. Compare while and Do – While loop with examples?
4. Compare break, continue, and goto statement?
5. What is pointer? Explain with example

 

Assignment-B
1. a) What is structured programming? Explain and give examples of relevant constructs using pseudo-code. Highlight the advantages and disadvantages of structured programming.
b) What is an execution error? Differentiate it from syntactic error. Give examples. c) It is said that ‘C’ is a middle level assembly language. Mention those features of ‘C’which enables this description. 2. a) Write and explain the action of WHILE statement. Develop a program in ‘C’ language to compute the average of every third integer number lying between 1 and 100. Include appropriate documentation. b) Develop a function to calculate sum of n even integers starting from a given even integer. c) Identify all the compound statements which appear in the following program segment: { sum=0; do { scanf(‚%d?, &i); if (i < 0) { i=-i; ++flag; } sum += i; } while (i != 0); } 3. a) Define an array. How are arrays processed in ‘C’? Illustrate by taking two-dimensional arrays as examples. b) What are subscripts? How are they specified? What restrictions apply to the values that can be assigned to subscripts in ‘C’ language? c) Write a ‘C’ program that will enter a line of text, store in an array and then display backwards. The length of the line should be undefined, (being terminated by ENTER key), but less than 80 characters.
Programming & Problem Solving through C Language
4. a) What is a pointer in ‘C’? How is a pointer variable declared? Give examples and explain.Enumerate the utility of pointer variables. b) A program in ‘C’ language contains the following declaration: static int x[8] = {1,2,3,4,5,6,7,8}; i) What is the meaning of x? ii) What is the meaning of (x + 2)? iii) What is the meaning of *x? iv) What is the meaning of (*x + 2)? v) What is the meaning of *(x + 2)? c) What is a structure? How does a structure differ from a union? Give examples. For what kind of applications, union data structure is useful? How are arrays different from structure? 5 a) How can a procedure be defined in ‘C’? Give an example. Bring out the differences between function and procedure. b) Draw a flowchart and then develop an interactive ‘C’ program which finds whether a given integer number is prime or not. Make use of a function subprogram.
Assignment-C
Multiple choice questions in C
/*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?
Choice 1 5 Choice 2 6 Choice 3 10 [Ans] Corrected by buddy by running the program Choice 4 11 Choice 5 12 – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – //*2*/ With every use of a memory allocation function, what function should be used to release allocated memory which is no longer needed? Choice 1 unalloc() Choice 2 dropmem() Choice 3 dealloc() Choice 4 release() Choice 5 free() [Ans] – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – //*3*/
Code:
void *ptr;
myStruct myArray[10];
ptr = myArray;
Which of the following is the correct way to increment the variable “ptr”? Choice 1 ptr = ptr + sizeof(myStruct); [Ans] Choice 2 ++(int*)ptr; Choice 3
Programming & Problem Solving through C Language
ptr = ptr + sizeof(myArray); Choice 4 increment(ptr); Choice 5 ptr = ptr + sizeof(ptr); – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – //*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;
}
What will print when the sample code above is executed? Choice 1 y = HELLO Choice 2 y = ELLO Choice 3 y = LLO Choice 4 y = LO [Ans] Choice 5 x = O – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – //*5*/
Code:
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? Choice 1 It will work correctly since the for loop covers the entire list. Choice 2 It may fail since each node “nPtr” is freed before its next address can be accessed. Choice 3 In the for loop, the assignment “nPtr=nPtr->next” should be changed to “nPtr=nPtr.next”.
Programming & Problem Solving through C Language
Choice 4 This is invalid syntax for freeing memory. Choice 5 The loop will never end. – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – //*6*/ What function will read a specified number of elements from a file? Choice 1 fileread() Choice 2 getline() Choice 3 readfile() Choice 4 fread() Choice 5 gets() – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – //*7*/ “My salary was increased by 15%!” Select the statement which will EXACTLY reproduce the line of text above. Choice 1 printf(“”My salary was increased by 15/%!”n”); Choice 2 printf(“My salary was increased by 15%!n”); Choice 3 printf(“My salary was increased by 15’%’!n”); Choice 4 printf(“”My salary was increased by 15%%!”n”);[Ans] Choice 5 printf(“”My salary was increased by 15’%’!”n”); – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – //*8*/ What is a difference between a declaration and a definition of a variable? Choice 1 Both can occur multiple times, but a declaration must occur first. Choice 2 There is no difference between them. Choice 3 A definition occurs once, but a declaration may occur many times. Choice 4 A declaration occurs once, but a definition may occur many times. [Ans] Choice 5 Both can occur multiple times, but a definition must occur first. – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – –
Programming & Problem Solving through C Language
//*9*/ 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? Choice 1 3 Choice 2 5 Choice 3 7 Choice 4 9 Choice 5 11[Ans] – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – //*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 Choice 1 12,10,11,13 Choice 2 22,10,11,13 Choice 3 22,11,11,11 Choice 4 12,11,11,11 Choice 5 22,13,13,13[Ans] – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – //*11*/
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? Choice 1 -3 Choice 2 0 Choice 3 4[Ans]
Programming & Problem Solving through C Language
Choice 4 4 + sizeof( int ) Choice 5 4 * sizeof( int – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – //*12*/
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? Choice 1 1, 2, 3, 4, 5, 5, Choice 2 4, 3, 2, 1, 0, 0, Choice 3 5, 4, 3, 2, 1, 0, Choice 4 0, 0, 1, 2, 3, 4, [Ans] Choice 5 0, 1, 2, 3, 4, 5, – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – //*13*/ 11 ^ 5 What does the operation shown above produce? Choice 1 1 Choice 2 6 Choice 3 8 Choice 4 14 [Ans] Choice 5 15 – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – –
Programming & Problem Solving through C Language
//*14*/ #define MAX_NUM 15 Referring to the sample above, what is MAX_NUM? Choice 1 MAX_NUM is an integer variable. Choice 2 MAX_NUM is a linker constant. Choice 3 MAX_NUM is a precompiler constant. Choice 4 MAX_NUM is a preprocessor macro. [Ans] Choice 5 MAX_NUM is an integer constant. – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – //*15*/ Which one of the following will turn off buffering for stdout? Choice 1 setbuf( stdout, FALSE ); Choice 2 setvbuf( stdout, NULL ); Choice 3 setbuf( stdout, NULL ); Choice 4 setvbuf( stdout, _IONBF ); Choice 5 setbuf( stdout, _IONBF ); – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – //*16*/ What is a proper method of opening a file for writing as binary file? Choice 1 FILE *f = fwrite( “test.bin”, “b” ); Choice 2 FILE *f = fopenb( “test.bin”, “w” ); Choice 3 FILE *f = fopen( “test.bin”, “wb” ); Choice 4 FILE *f = fwriteb( “test.bin” ); Choice 5 FILE *f = fopen( “test.bin”, “bw” ); – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – //*17*/ 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? Choice 1 memcpy()
Programming & Problem Solving through C Language
Choice 2 memset() Choice 3 strncpy() Choice 4 strcpy() Choice 5 memmove()[Ans] – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – //*18*/ int x = 2 * 3 + 4 * 5; What value will x contain in the sample code above? Choice 1 22 Choice 2 26[Ans] Choice 3 46 Choice 4 50 Choice 5 70 – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – //*19*/
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? Choice 1 If the arguments to memcpy() refer to overlapping regions, the destination buffer will be subject to memory corruption. Choice 2 array_dup() declares its first parameter to be a pointer, when the actual argument will be an array.
Programming & Problem Solving through C Language
Choice 3 The memory obtained from alloca() is not valid in the context of the caller. Moreover, alloca() is nonstandard. Choice 4 size_t is not a Standard C defined type, and may not be known to the compiler. Choice 5 The definition of array_dup() is unusual. Functions cannot be defined using this syntax. – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – //*20*/ int var1; If a variable has been declared with file scope, as above, can it safely be accessed globally from another file? Choice 1 Yes; it can be referenced through the register specifier. Choice 2 No; it would have to have been initially declared as a static variable. Choice 3 No; it would need to have been initially declared using the global keyword.[Ans] Choice 4 Yes; it can be referenced through the publish specifier. Choice 5 Yes; it can be referenced through the extern specifier. – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – //*21*/ time_t t; Which one of the following statements will properly initialize the variable t with the current time from the sample above? Choice 1 t = clock();[Ans] Choice 2 time( &t ); Choice 3 t = ctime(); Choice 4 t = localtime(); Choice 5 None of the above – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – //*22*/ Which one of the following provides conceptual support for function calls? Choice 1 The system stack[Ans] Choice 2 The data segment Choice 3 The processor’s registers
Programming & Problem Solving through C Language
Choice 4 The text segment Choice 5 The heap – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – //*23*/ C is which kind of language? Choice 1 Machine Choice 2 Procedural[Ans] Choice 3 Assembly Choice 4 Object-oriented Choice 5 Strictly-typed – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – //*24*/
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? Choice 1 1 Choice 2 2 Choice 3 3 Choice 4 4 Choice 5 5 [Ans] 00,10,01,11,12 – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – //*25*/
Code:
int x = 0;
for (x=1; x<4; x++);
printf(“x=%dn”, x);
What will be printed when the sample code above is executed? Choice 1
Programming & Problem Solving through C Language
x=0 Choice 2 x=1 Choice 3 x=3 Choice 4 x=4[Ans] Choice 5 x=5 – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – //*26*/
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? Choice 1 1 Choice 2 2[Ans] Choice 3 3 Choice 4 4 Choice 5 5 – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – //*27*/
Code:
char *ptr;
char myString[] = “abcdefg”;
ptr = myString;
ptr += 5;
What string does ptr point to in the sample code above? Choice 1 fg [Ans]/*because string*/ Choice 2 efg Choice 3 defg Choice 4 cdefg Choice 5 None of the above – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – –
Programming & Problem Solving through C Language
//*28*/
Code:
double x = -3.5, y = 3.5;
printf( “%.0f : %.0fn”, ceil( x ), ceil( y ) );
printf( “%.0f : %.0fn”, floor( x ), floor( y ) );
What will the code above print when executed? ceil =>rounds up 3.2=4 floor =>rounds down 3.2=3 Choice 1 -3 : 4 -4 : 3 [Ans] Choice 2 -4 : 4 -3 : 3 Choice 3 -4 : 3 -4 : 3 Choice 4 -4 : 3 -3 : 4 Choice 5 -3 : 3 -4 : 4 – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – //*29*/ Which one of the following will declare a pointer to an integer at address 0x200 in memory? Choice 1 int *x; *x = 0x200;[Ans] Choice 2 int *x = &0x200; Choice 3 int *x = *0x200; Choice 4 int *x = 0x200; Choice 5 int *x( &0x200 ); – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – //*30*/
Code:
int x = 5;
int y = 2;
char op = ‘*’;
switch (op)
{
default : x += 1;
case ‘+’ : x += y; /*It will go to all the cases*/
Programming & Problem Solving through C Language
case ‘-‘ : x -= y;
}
After the sample code above has been executed, what value will the variable x contain? Choice 1 4 Choice 2 5 Choice 3 6 [Ans] Choice 4 7 Choice 5 8 – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – //*31*/
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? Choice 1 0 Choice 2 1 Choice 3 2[Ans] Choice 4 3 Choice 5 4 – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – //*32*/ char ** array [12][12][12]; Consider array, defined above. Which one of the following definitions and initializations of p is valid? Choice 1 char ** (* p) [12][12] = array; [Ans] Choice 2 char ***** p = array; Choice 3 char * (* p) [12][12][12] = array; Choice 4 const char ** p [12][12][12] = array;
Programming & Problem Solving through C Language
Choice 5 char (** p) [12][12] = array;
– – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – –
//*33*/ 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); Choice 1 typedef void (*sighandler_t) (int);[Ans] Choice 2 typedef sighandler_t void (*) (int); Choice 3 typedef void *sighandler_t (int); Choice 4 #define sighandler_t(x) void (*x) (int) Choice 5 #define sighandler_t void (*) (int) – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – //*34*/ All of the following choices represent syntactically correct function definitions. Which one of the following represents a semantically legal function definition in Standard C? Choice 1
Code:
int count_digits (const char * buf) {
assert(buf != NULL);
int cnt = 0, i;
for (i = 0; buf[i] != ‘ ‘; i++)
if (isdigit(buf[i]))
cnt++;
return cnt;
}
Choice 2
Code:
int count_digits (const char * buf) {
int cnt = 0;
assert(buf != NULL);
for (int i = 0; buf[i] != ‘ ‘; i++)
if (isdigit(buf[i]))
cnt++;
return cnt;
}
Choice 3
Code:
int count_digits (const char * buf) {
int cnt = 0, i;
Programming & Problem Solving through C Language
assert(buf != NULL);
for (i = 0; buf[i] != ‘ ‘; i++)
if (isdigit(buf[i]))
cnt++;
return cnt;
}
Choice 4
Code:
int count_digits (const char * buf) {
assert(buf != NULL);
for (i = 0; buf[i] != ‘ ‘; i++)
if (isdigit(buf[i]))
cnt++;
return cnt;
}
Choice 5
Code:
int count_digits (const char * buf) {
assert(buf != NULL);
int cnt = 0;
for (int i = 0; buf[i] != ‘ ‘; i++)
if (isdigit(buf[i]))
cnt++;
return cnt;
}
– – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – –
//*35*/ 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?
Choice 1 ptr = realloc( ptr, 10 * sizeof( struct customer)); [Ans] Choice 2 realloc( ptr, 9 * sizeof( struct customer ) );
Choice 3 ptr += malloc( 9 * sizeof( struct customer ) ); Choice 4 ptr = realloc( ptr, 9 * sizeof( struct customer ) );
Choice 5 realloc( ptr, 10 * sizeof( struct customer ) ); – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – –

//*36*/ Which one of the following is a true statement about pointers? Choice 1 Pointer arithmetic is permitted on pointers of any type. Choice 2 A pointer of type void * can be used to directly examine or modify an object of any type.
Choice 3 Standard C mandates a minimum of four levels of indirection accessible through a pointer. Choice 4 A C program knows the types of its pointers and indirectly referenced data items at runtime. Choice 5 Pointers may be used to simulate call-by-reference. – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – –
//*37*/ Which one of the following functions returns the string representation from a pointer to a time_t value? Choice 1 localtime Choice 2 gmtime Choice 3 strtime Choice 4 asctime Choice 5 ctime
– – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – –
//*38*/
Code:
short testarray[4][3] = { {1}, {2, 3}, {4, 5, 6} };
printf( “%dn”, sizeof( testarray ) );
Assuming a short is two bytes long, what will be printed by the above code?
Choice 1 It will not compile because not enough initializers are given.
Choice 2 6
Choice 3 7 Choice 4 12 Choice 5 24 [Ans]
– – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – –
//*39*/ char buf [] = “Hello world!”; char * buf = “Hello world!”; In terms of code generation, how do the two definitions of buf, both presented above, differ? Choice 1
The first definition certainly allows the contents of buf to be safely modified at runtime; the second definition does not. Choice 2 The first definition is not suitable for usage as an argument to a function call; the second definition is. Choice 3 The first definition is not legal because it does not indicate the size of the array to be allocated; the second definition is legal. Choice 4 They do not differ — they are functionally equivalent. [Ans] Choice 5 The first definition does not allocate enough space for a terminating NUL-character, nor does it append one; the second definition does. – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – –

//*40*/ In a C expression, how is a logical AND represented?
Choice 1 @@ Choice 2 || Choice 3 .AND. Choice 4 && [Ans]
Choice 5 .AND – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – –

Contact www.solvedcare.com for best and lowest cost solution or email solvedcare@gmail.com

Comments are closed.