Tuesday, February 11, 2020

1.5. IO statement in C


Session 5 

1. What does this statement printf(“%10s”, state); means?

A) 10 spaces before the string state is printed

B) Print empty spaces if the string state is less than 10 characters

C) Print the last 10 characters of the string

D) None of the mentioned

ANSWER: B



2. What is the output of this C code?

#include <stdio.h>

int main()

{

int i = 10, j = 3;

printf("%d %d %d", i, j);

}

A) Compile time error

B) 10 3

C) 10 3 some garbage value

D) Undefined behaviour

ANSWER: C

EXPLANATION: there is no value assigned to the third %d in the printf. So it assumes a garbage value



3. What is the output of this C code?

#include <stdio.h>

int main()

{

int i = 10, j = 3, k = 3;

printf("%d %d ", i, j, k);

}

A) Compile time error

B) 10 3 3

C) 10 3

D) 10 3 somegarbage value

ANSWER: C

EXPLANATION: Even though k is defined, a %d is not mentioned for it in the printf statement. So it wont be printed at all



4. What is the output of this C code?

#include <stdio.h>

int main()

{

char *s = "myworld";

int i = 9;

printf("%*s", i, s);

}

A) myworld

B) myworld(note: spaces to the left of myworlD)

C) myworld (note:followed by two spaces after myworlD)

D) Undefined

ANSWER: B

EXPLANATION: As there are only 7 letters, the output would be preceded with 2 spaces to fill the entire slot of 9



5. #include<stdio.h>

void main()

{

clrscr();

printf("%d",printf("CQUESTIONBANK"));

getch();

}



What will output when you compile and run the above code?

A)13CQUESTIONBANK

B)CQUESTIONBANK13

C)Garbage CQUESTIONBANK

D)Compiler error

ANSWER: B

EXPLANATION: Inner printf is executed first as printf is executed from right to left. As the outer printf prints and integer value, it prints the length of the string.



6. #include<stdio.h>

#include<conio.h>

void main()

{

int a=5,b=6,c=11;

clrscr();

printf("%d %d %d");

getch();

}

What will output when you compile and run the above code?

A)Garbage value garbage value garbage value

B)5 6 11

C)11 6 5

D)Compiler error

ANSWER: C

EXPLANATION: The values 5,6,11 are stored in a stack format and 11 being the last number to enter the stack, is the first to be popped. So it is printed first

7. What will happen after compiling and running following code?

main()

{

printf("%p", main);

}

A. Error

B. Will make an infinite loop.

C. Some address will be printed.

D. None of these.

ANSWER: C

EXPLANATION: Function names are just addresses (just like array names are addresses).

main() is also a function. So the address of function main will be printed. %p in printf specifies that the argument is an address. They will be printed as hexadecimal numbers.



8. The default parameter passing mechanism is

A. call by value

B. call by reference

C. call by value result

D. None of these.

ANSWER: A



9. What is the result of compiling and running this code?

main()

{

char string[] = "Hello World";

display(string);

}



void display(char *string)

{

printf("%s", string);

}

A. will print Hello World

B. Compilation Error

C. will print garbage value

D. None of these.

ANSWER: B

EXPLANATION: Compiler Error : Type mismatch in redeclaration of function display

As the function display() is not defined before use, compiler will assume the return type of the function which is int(default return type). But when compiler will see the actual definition of display(), mismatch occurs since the function display() is declared as void. Hence the error.



10. Pick the correct statements.

I. The body of a function should have only one return statement.

II. The body of a function may have many return statements.

III. A function can return only one value to the calling environment.

IV. If return statement is omitted, then the function does its job but returns no value to the calling environment.



A. I and II

B. I and III

C. II and III

D. II and IV

E. III and IV

ANSWER: C



11. A program that has no command line arguments will have argc

A) Zero

B) Negative

C) One

D) Two

ANSWER: C

EXPLANATION: The minimum argc value is 1 ie when no command line arguements are present



12. What is the output of this C code (run without any commandline arguments)?



#include <stdio.h>

int main(int argc, char *argv[])

{

printf("%s\n", argv[argc]);

return 0;

}

A) Segmentation fault/code crash

B) Executable file name

C) Depends on the platform

D) Depends on the compiler

ANSWER: A

EXPLANATION: There are no values for argc which can be used for printing.



13. Which function is not called in the following program?



#include <stdio.h>

void first()

{

printf("first");

}

void second()

{

first();

}

void third()

{

second();

}

void main()

{

void (*ptr)();

ptr = third;

ptr();

}

A) Function first

B) Function second

C) Function third

D) None of the mentioned

ANSWER: D

EXPLANATION: Each of the function is called atleast once



14. Correct syntax to pass a Function Pointer as an argument

A) void pass(int (*fptr)(int, float, char)){}

B) void pass(*fptr(int, float, char)){}

C) void pass(int (*fptr)){}

D) void pass(*fptr){}

ANSWER: A



15. Which of the following is not possible in C?

A) Array of function pointer

B) Returning a function pointer

C) Comparison of function pointer

D) None of the mentioned

ANSWER: D

EXPLANATION: All are possible



16. What is the output of this C code?



#include <stdio.h>

int mul(int a, int b, int C)

{

return a * b * c;

}

void main()

{

int (*function_pointer)(int, int, int);

function_pointer = mul;

printf("The product of three numbers is:%d",

function_pointer(2, 3, 4));

}

A) The product of three numbers is:24

B) Run time error

C) Nothing

D) Varies

ANSWER: A

EXPLANATION: The function pointer is initialized in a valid manner and the values 2,3,4 are passed as parameters to the funciton mul().



17. What is the output of this C code?



#include <stdio.h>

int main()

{

int n;

scanf("%d", n);

printf("%d\n", n);

return 0;

}

A) Compilation error

B) Undefined behavior

C) Whatever user types

D) Depends on the standard

ANSWER: B

EXPLANATION: scanf statement does not have an & to scan the value of n which is an integer .



18. What is the output of this C code?



#include <stdio.h>

int main()

{

char n[] = "hellonworld!";

char s[13];

sscanf(n, "%s", s);

printf("%s\n", s);

return 0;

}

A) hellonworld!

B) hello

world!

C) hello

D) hello world!

ANSWER: C

EXPLANATION: the sscanf scans for the letter n in the string "hellonworld" and it truncates this string upto the letter n and stores it in s. So output is c



19. Can we use a function as a parameter of another function? [ Eg: void wow(int func()) ]

A) Yes, and we can use the function value conveniently

B) Yes, but we call the function again to get the value, not as convenient as in using variable

C) No, C does not support it.

D) This case is compiler dependent

ANSWER: C



20. int sum(int a, int B)

A) return (a + B);

B) int sum(int a, int B)

{return (a + B);}

C) int sum(a, B)

return (a + B);

D) Both (A) and (B)

ANSWER: B



21. Which of the following function declaration is illegal?

A) double func();

int main(){}

double func(){}

B) double func(){};

int main(){}

C) int main()

{

double func();

}

double func(){//statements}

D) None of the mentioned

ANSWER: D

EXPLANATION: Others are valid declarations of functions and will be accepted by the compiler



22. What is the problem in the following declarations?

int func(int);

double func(int);

int func(float);

A) A function with same name cannot have different signatures

B) A function with same name cannot have different return types

C) A function with same name cannot have different number of parameters

D) All of the mentioned

ANSWER: D



23. The output of the code below is



#include <stdio.h>

int *m();

void main()

{

int k = m();

printf("%d", k);

}

int *m()

{

int a[2] = {5, 8};

return a;

}

A) 5

B) 8

C) Nothing

D) Varies

ANSWER: D

EXPLANATION: The pointer function could point to any of the values and return any value hence the output would vary accordingly.



24. functions can return structure in c?

A) true

B) false

C) Depends on the compiler

D) Depends on the standard

ANSWER: A



25. functions can return enumeration constants in c?

A) true

B) false

C) depends on the compiler

D) depends on the standard

ANSWER: A

26. What is the output of this C code?

#include <stdio.h>

double foo();

int main()

{

foo();

return 0;

}

foo()

{

printf("2 ");

return 2;

}

A) 2

B) Compile time error

C) Depends on the compiler

D) Depends on the standard

ANSWER: B

EXPLANATION: The function cannot return and print a value at the same time.



27. fputs function writes a string to a file that only ends with a newline.

A) true

B) false

C) Depends on the standard

D) Depends on the compiler

ANSWER: B



28. What will be the output of the following program code?

main()

{

static int var = 5;

printf("%d ", var--);

if(var)

main();

}

A. 5 5 5 5 5

B. 5 4 3 2 1

C. Infinite Loop

D. Compilation Error

E. None of these

ANSWER: B

EXPLANATION: When static storage class is given, it is initialized once. The change in the value of a static variable is retained even between the function calls. Main is also treated like any other ordinary function, which can be called recursively.



29. What will be printed when this program is executed?

int f(int x)

{

if(x <= 4)

return x;

return f(--x);

}

void main()

{

printf("%d ", f(7));

}

A. 4 5 6 7

B. 1 2 3 4

C. 4

D. Syntax error

E. Runtime error

ANSWER: C

EXPLANATION: In this recursive function call the function will return to main caller when the value of x is 4. Hence the output.



30. The recursive functions are executed in a __________

A. Parallel order

B. First In First Out order

C. Last In First Out order

D. Iterative order

E. Random order

ANSWER: C

EXPLANATION: Because for each function call an entry is created in stack frame( known as Active Record Instance), and are executed in LIFO manner.



31. The function scanf() returns ___________

A. The actual values read for each argument.

B. 1

C. 0

D. The number of successful read input values.

E. ASCII value of the input read.

ANSWER: D

EXPLANATION: According to the prototype, the return type of scanf( ) function is int, that is the number of successful read input values.



32. Functions have _____________

A. Local scope

B. Block scope

C. File scope

D. Function scope

E. No scope at all

ANSWER: C



33. char* myfunc(char *ptr)

{

ptr+=3;

return(ptr);

}



void main()

{

char *x, *y;

x = "EXAMVEDA";

y = myfunc(x);

printf("y=%s", y);

}

What will be printed when the sample code above is executed?

A. y=EXAMVEDA

B. y=MVEDA

C. y=VEDA

D. y=EDA

E. y=AMVEDA

ANSWER: B

EXPLANATION: Ptr=ptr+3 implies that the string in x is printed after the third letter.



34. Which of the following is an invalid method for input?

A) scanf(“%d%d%d”,&a, &b, &C);

B) scanf(“%d %d %d”, &a, &b, &C);

C) scanf(“Three values are %d %d %d”,&a,&b,&C);

D) None of the mentioned

ANSWER: D



35. Which of the following represents the function for scanf?

A) void scanf(char *format, …)

B) int scanf(char *format, …)

C) char scanf(int format, …)

D) char *scanf(char *format, …)

ANSWER: B

EXPLANATION: Scanf returns the number of successfully matched and assigned input items.



36. Escape sequences are prefixed with.

A) %

B) /

C) ”

D) None of the mentioned

ANSWER: D



37. putchar(C) function/macro always outputs character c to the

A) screen

B) standard output

C) depends on the compiler

D) Depends on the standard

ANSWER: B



38. What is the output of this C code?



#include <stdio.h>

int main()

{

int i = 11;

int *p = &i;

foo(&p);

printf("%d ", *p);

}

void foo(int *const *p)

{

int j = 10;

*p = &j;

printf("%d ", **p);

}

A) Compile time error

B) 10 10

C) Undefined behaviour

D) 10 11

ANSWER: A

EXPLANATION: No proper value for p is obtained and ambiguity occurs while assigning pointer p.



39. Which of the following can never be sent by call-by-value?

A) Variable

B) Array

C) Structures

D) Both (B) and (C)

ANSWER: B



40. #include<stdio.h>

#include<conio.h>

void main()

{

short int a=5;

clrscr();

printf("%d"+1,A);

getch();

}



What will output when you compile and run the above code?

A)6

B)51

C)d

D)Compiler error

ANSWER: C

EXPLANATION: +1 will truncate the first character in %d hence it is left with printf("d",A) as there are no format specifiers it prints d



41. How to call a function without using the function name to send parameters?

A) typedefs

B) Function pointer

C) Both (A) and (B)

D) None of the mentioned

ANSWER: B

EXPLANATION: Instead of referring to data values, a function pointer points to executable code within memory. When dereferenced, a function pointer can be used to invoke the function it points to and pass its arguments just like a normal function call.



42. Which of the following data-types are promoted when used as a parameter for an ellipsis?

A) char

B) short

C) int

D) None of the mentioned

ANSWER: A

EXPLANATION: It takes the axis values.



43. The type va_list is used in an argument list

A) To declare a variable that will refer to each argument in turn;

B) For cleanup

C) To create a list

D) There is no such type

ANSWER: A

EXPLANATION: va_list is generally used in variable arguement functions(VARARGS) and is used to refer each arguement.



44. The index of the last argument in command line arguments is

A) argc – 2

B) argc + 1

C) argc

D) argc – 1

ANSWER: D

EXPLANATION: the indices in the array argc ranges from 0 to n-1.



45. What type of array is generally generated in Command-line argument?

A) Single dimension array

B) 2-Dimensional Square Array

C) Jagged Array

D) 2-Dimensional Rectangular Array

ANSWER: C

EXPLANATION: A jagged array is an array whose elements are arrays. The elements of a jagged array can be of different dimensions and sizes. A jagged array is sometimes called an "array of arrays." The following examples show how to declare, initialize, and access jagged arrays.



46. Use of functions

A. Helps to avoid repeating a set of statements many times.

B. Enhances the logical clarity of the program.

C. Helps to avoid repeated programming across programs.

D. Makes the debugging task easier.

E. All of the above

ANSWER: E



47. Determine output:

main()

{

int i = 5;

printf("%d%d%d%d%d", i++, i--, ++i, --i, i);

}

A. 54544

B. 45445

C. 54554

D. 45545

ANSWER: D

EXPLANATION: The arguments in a function call are pushed into the stack from left to right. The evaluation is by popping out from the stack. and the evaluation is from right to left, hence the result



48. The second (argument vector) in command line arguments is

A) The number of command-line arguments the program was invoked with;

B) A pointer to an array of character strings that contain the arguments,one per string.

C) Nothing

D) Both a & b

ANSWER: B



49. argv[0] in command line arguments, is

A) The name by which the program was invoked

B) The name of the files which are passed to the program

C) Count of the arguments in argv[] vector

D) Both a & b

ANSWER: A



50. The output of the code below is



#include <stdio.h>

void m(int k)

{

printf("hi");

}

void m(double k)

{

printf("hello");

}

void main()

{

m(3);

}

A) hi

B) hello

C) Compile time error

D) Nothing

ANSWER: C

EXPLANATION: There is ambiguity when we call the function m. m() is an overloaded function with different parameter types. But when m is called it sees an ambiguity in the two functions.


Previous                  Next

No comments:

Post a Comment