Database MCQ Questions :
Test Your C Skills.
CHAPTER 1: DECLARATIONS AND INITIALIZATION
Q1> WHAT IS THE O/P?
main()
{
char far *s1,*s2;
printf(""%d%d",sizeof(s1),sizeof(s2));
}
answer>4 2
Q.2>What will be the o/p?
int x=40;
main()
{
int x=20;
printf("%d",x);
}
answer>20
Q.3>What will be the o/p?
main()
{
int x=40;
{
int x=20;
printf("%d",x);
}
printf("%d",x);
}
answer>20 40
Q.4>Is the following statement declaration or defination?
extern int x;
answer> declaration
Q.5> What is the o/p?
main()
{
extern int i;
i=20;
printf("%d",sizeof(i));
}
answer> error,i undefined because extern int i is a declaration and not defination
Q.6>Is it true that the global variable have many declarations but only one defination?
answer> yes
Q.7>Is it true that the function may have many decalaratins but only one defination?
answer> yes
Q.8>In the following program where the variable a is geting defined and where it is declared?
main()
{
extern int a; /* declaration*/
printf("%d",a0;
}
int a=12; /* defination*/
Q.9>What will be the o/p of above program?
answer>20
Q.10>What is the difference between declaration and defination of a variable?
answer> declaration:-only gives the type,status and nature of variable without reserving any space for the variable defination;-actual space is reserverd for the variable and some initial value is given.
Q.11>if the defination of the external variable occurs in the source file before it's use in a perticular function then there is no need for an external declaration in the function?
answer> true
Q.12>suppose the program is devided in three source files f1,f2,f3 and the variable is defined in file f1 but used in f2 and f3. In such a casewould we need the external declaration for the variable in files f2 and f3?
answer>yes
Q.13>when we mention the prototype of the function ,we are definig it or declaring it?
answer> declaring it
Q.14>what is the difference between following declarations
extern int fun()
int fun();
answer> nothing except that that the firat one gives us hint that function fun is probally in another file.
Q.15>why does the following programreports the redeclaration error of function display()
main()
{
dispaly();
}
void dispaly()
{
printf("fggagaetaertrt");
}
answer> here the function dispay() is called before it is declared .That is why the complier assumes it to be declared as int display(); that accept unspecified no of arguments.i.e. undeclared function assumes to return int on appering the declaration the fun shows that it returns void hence the error.
Q.16> What will be the o/p?
main()
{
extern int fun(float);
int a;
a=fun(3.14);
printf("%d",a);
}
int fun(aa) /* K & R style of function defenation*/
float aa
{
return((int)aa);
}
answer> error, because we have mixed the ansi prototype with k & r style of function defenation If we use an ANSI prototype and pass float to the function then it is promoted to double the funcction accepts it in to variable of type float hence the type mismatch occurs To remady the situation define the function as
int fun(float aa)
{
.....
}
Q.17>Point error if any:
struct emp
{
char name[20];
int age;
}
fun(int aa)
{
int bb;
bb=aa*aa;
return(bb);
}
main()
{
int a;
a=fun(20);
printf("%d",a);
}
answer> missing semicollon at the end of struct due to which the function fun assumed to be returning vsr of type struct emp. but it returns an int hence the error.
Q.18> If you are to share the variables or functions across several source files how would you enshore that all definications and declarations are consistant?
answer> The best arrangement is to place each defination in a revelent .c file , then put an external declaration in a header file (.h file) and use #includeto briang the declaration wherever needed. The .c file which contains the definations should also include the header file, so that the complier can check that the defination matches the declaration.
Q.19> Correct the error:
f(struct emp0;
struct emp
{
char name[20];
int age;
};
main()
{
struct empe={"Vivek",21}
f(e);
}
f(struct emp ee)
{
printf("\n %s %d",ee.name,ee.age);
}
answer> declare the structure before the prototype of f.
Q.20> Global variables are available to all functions. Does there exist a mechanism by way of which I can make it available to some and not to others.
answer>NO.
Q.21>What do you mean by a translation unit?
answer> A trnslation unit is a set of source files as seen by the complier and translated as a unit. Generally one .c file plus all header files mentioned in the #include directives.
Q.22>What wouldbe the output of the following program
main()
{
int a[5]={2,3}
printf("\n %d %d %d",a[2],a[3],a[4]);
}
answer> 0 0 0 if a automatic array is partially initialised then remaiing elements are initialised by 0
Q.23>What will be the output :
main()
{
struct emp
{
char name[20];
int age;
float sal;
};
struct emp e={"vivek"}
printf("\n %d %f",e.age, e.sal);
}
answer>0 0.000000 if an automatic structure is partially initialised then remaining elements are initialised bu 0.
Q.24>Some books sugget that the fillowing definations should be preceded by the word static. Is it correct?
int a[]={2,3,4,12,32}
struct emp e={"vinod",23}
answer> pre ANSI compilers has such requirment but compliers confirming to ANSI standard does not have such requirment.
Q.25>Point out error
main()
{
int(*p)()=fun;
(*p)();
}
fun()
{
printf("\n Loud and clear");
}
answer> Here we are initialising function pointer to address of the function fun() but during the time of initialisation the function has not been defined. Hence an error To eliminate the error add the prototype of function fun() before the declaration of p, as shown bellow;
extern int fun(); or simply int fun();
Q.26> point error if any
main()
{
union a
{
int i;
char ch[2];
};
union a z=512;
printf("%d %d",z.ch[0],z.ch[1]);
}
answer> In pre-ANSI complier union vriable can not be initialised . ANSI complier permits initialisation of first member of the union.
Q.27> What do you mean by the scope of the variable? what are the 4 differet types of scopes that a variables can have?
answer> Scope indicates the region over which the variable's declaration has an effect. The four kinds of scopes are: file function,block,prototype.
Q.28> What are different types of linkages?
answer> There are three different types of linkages : external , internal , and none. External linkage means global, non-static variables and functions, internal linkage means static variables and functions with file scope and no linkage means local variables.
Q1> WHAT IS THE O/P?
main()
{
char far *s1,*s2;
printf(""%d%d",sizeof(s1),sizeof(s2));
}
answer>4 2
Q.2>What will be the o/p?
int x=40;
main()
{
int x=20;
printf("%d",x);
}
answer>20
Q.3>What will be the o/p?
main()
{
int x=40;
{
int x=20;
printf("%d",x);
}
printf("%d",x);
}
answer>20 40
Q.4>Is the following statement declaration or defination?
extern int x;
answer> declaration
Q.5> What is the o/p?
main()
{
extern int i;
i=20;
printf("%d",sizeof(i));
}
answer> error,i undefined because extern int i is a declaration and not defination
Q.6>Is it true that the global variable have many declarations but only one defination?
answer> yes
Q.7>Is it true that the function may have many decalaratins but only one defination?
answer> yes
Q.8>In the following program where the variable a is geting defined and where it is declared?
main()
{
extern int a; /* declaration*/
printf("%d",a0;
}
int a=12; /* defination*/
Q.9>What will be the o/p of above program?
answer>20
Q.10>What is the difference between declaration and defination of a variable?
answer> declaration:-only gives the type,status and nature of variable without reserving any space for the variable defination;-actual space is reserverd for the variable and some initial value is given.
Q.11>if the defination of the external variable occurs in the source file before it's use in a perticular function then there is no need for an external declaration in the function?
answer> true
Q.12>suppose the program is devided in three source files f1,f2,f3 and the variable is defined in file f1 but used in f2 and f3. In such a casewould we need the external declaration for the variable in files f2 and f3?
answer>yes
Q.13>when we mention the prototype of the function ,we are definig it or declaring it?
answer> declaring it
Q.14>what is the difference between following declarations
extern int fun()
int fun();
answer> nothing except that that the firat one gives us hint that function fun is probally in another file.
Q.15>why does the following programreports the redeclaration error of function display()
main()
{
dispaly();
}
void dispaly()
{
printf("fggagaetaertrt");
}
answer> here the function dispay() is called before it is declared .That is why the complier assumes it to be declared as int display(); that accept unspecified no of arguments.i.e. undeclared function assumes to return int on appering the declaration the fun shows that it returns void hence the error.
Q.16> What will be the o/p?
main()
{
extern int fun(float);
int a;
a=fun(3.14);
printf("%d",a);
}
int fun(aa) /* K & R style of function defenation*/
float aa
{
return((int)aa);
}
answer> error, because we have mixed the ansi prototype with k & r style of function defenation If we use an ANSI prototype and pass float to the function then it is promoted to double the funcction accepts it in to variable of type float hence the type mismatch occurs To remady the situation define the function as
int fun(float aa)
{
.....
}
Q.17>Point error if any:
struct emp
{
char name[20];
int age;
}
fun(int aa)
{
int bb;
bb=aa*aa;
return(bb);
}
main()
{
int a;
a=fun(20);
printf("%d",a);
}
answer> missing semicollon at the end of struct due to which the function fun assumed to be returning vsr of type struct emp. but it returns an int hence the error.
Q.18> If you are to share the variables or functions across several source files how would you enshore that all definications and declarations are consistant?
answer> The best arrangement is to place each defination in a revelent .c file , then put an external declaration in a header file (.h file) and use #includeto briang the declaration wherever needed. The .c file which contains the definations should also include the header file, so that the complier can check that the defination matches the declaration.
Q.19> Correct the error:
f(struct emp0;
struct emp
{
char name[20];
int age;
};
main()
{
struct empe={"Vivek",21}
f(e);
}
f(struct emp ee)
{
printf("\n %s %d",ee.name,ee.age);
}
answer> declare the structure before the prototype of f.
Q.20> Global variables are available to all functions. Does there exist a mechanism by way of which I can make it available to some and not to others.
answer>NO.
Q.21>What do you mean by a translation unit?
answer> A trnslation unit is a set of source files as seen by the complier and translated as a unit. Generally one .c file plus all header files mentioned in the #include directives.
Q.22>What wouldbe the output of the following program
main()
{
int a[5]={2,3}
printf("\n %d %d %d",a[2],a[3],a[4]);
}
answer> 0 0 0 if a automatic array is partially initialised then remaiing elements are initialised by 0
Q.23>What will be the output :
main()
{
struct emp
{
char name[20];
int age;
float sal;
};
struct emp e={"vivek"}
printf("\n %d %f",e.age, e.sal);
}
answer>0 0.000000 if an automatic structure is partially initialised then remaining elements are initialised bu 0.
Q.24>Some books sugget that the fillowing definations should be preceded by the word static. Is it correct?
int a[]={2,3,4,12,32}
struct emp e={"vinod",23}
answer> pre ANSI compilers has such requirment but compliers confirming to ANSI standard does not have such requirment.
Q.25>Point out error
main()
{
int(*p)()=fun;
(*p)();
}
fun()
{
printf("\n Loud and clear");
}
answer> Here we are initialising function pointer to address of the function fun() but during the time of initialisation the function has not been defined. Hence an error To eliminate the error add the prototype of function fun() before the declaration of p, as shown bellow;
extern int fun(); or simply int fun();
Q.26> point error if any
main()
{
union a
{
int i;
char ch[2];
};
union a z=512;
printf("%d %d",z.ch[0],z.ch[1]);
}
answer> In pre-ANSI complier union vriable can not be initialised . ANSI complier permits initialisation of first member of the union.
Q.27> What do you mean by the scope of the variable? what are the 4 differet types of scopes that a variables can have?
answer> Scope indicates the region over which the variable's declaration has an effect. The four kinds of scopes are: file function,block,prototype.
Q.28> What are different types of linkages?
answer> There are three different types of linkages : external , internal , and none. External linkage means global, non-static variables and functions, internal linkage means static variables and functions with file scope and no linkage means local variables.
SOME MCQ FOR U for Practice.......
#######################################################################
2.QUESTIONS
#######################################################################
1. Base class has some virtual method and derived class has a method with the same name. If we initialize the base class pointer with derived object; calling of that virtual method will result in which method being called?
a. Base method b. Derived method.
c. Error d. None of these
2. Given below are two sets of statements:(a) & (b). Select the correct observation:
a> const char *p="Hello"; p="World";
b> char * const p="Hello"; p[1]='P';
A. Both a & b are correct since non-const data/pointer is being changed in both cases.
B. Both a & b are wrong since const data/pointer cannot be changed once assigned.
C. a is correct, b is wrong.
D. a is wrong, b is correct.
3. void main ()
{
"Bunka"[2]='P';
}
Select the correct statement:
A. Error: lvalue required
B. Error: A string literal is a const char * and hence cannot be modified directly.
C. A string literal has the data type char * and hence cannot be modified.
D. The code compiles but memory access error at run-time.
4. Select all the correct statements about "Abstraction"
A. Assumes essential characteristics of an object to distinguish it from other kinds of objects.
B. Provides a simplified view of the object.
C. It separates the implementation from its interface.
D. It is the enforcement of the type class of an object.
5. The behavior of a class is decided by
A. Data members. B. Methods. C. Access permissions. D. All of the above.
6. Which statement(s) is/are true about "Static Typing"?
A. Refers to Types declared at compile time.
B. More efficient and reliable than dynamic typing.
C. Can be used to implement polymorphism.
D. Refers to all static data/methods in your classes.
7. Which statements(s) is/are true about Dynamic binding?
A. The correct function to call is resolved at run-time.
B. These functions are statically typed.
C. These functions are dynamically typed.
D. Refers to all dynamically allocated data in your classes.
8. Reusability of objects is achieved by
A. Inheritance. B. Aggregation. C. Polymorphism. D. All of the above.
9. Which statements(s) is/are true about "Hierarchy" of objects?
A. It is an ordering of abstractions.
B. It refers to "is a " relationship expressed by inheritance.
C. It refers to "has a" relationship expressed by aggregation.
D. It refers to access permissions on the aggregated object within an object.
10. What are "Generic classes"? (Select all that are correct)
A. A universal base class to cater to a wide range of derived classes.
B. A parameterized class, which is reusable across, types.
C. Used where implementation and behavior is independent of type.
D. Also known as abstract classes.
11. A language is "Object Base" if (Select all that are correct)
A. It supports abstraction. B. It has a concept of class.
C. It provides direct support for inheritance. D. All of the above.
12. "Object Persistence" covers which of the following (Select all that are correct)
A. Global variables & heap items. B. Object data storage.
C. Object type storage. D. All of the above.
13. Which statement(s) is/are true about shared inheritance?
A. All base classes are by default example of shared inheritance.
B. In multiple inheritances the derived classes share a single instance of the Base Class.
C. The base class is declared dynamically & there can be only one instance of it.
D. It allows distributed objects to share remote object and treat it as their own.
14. void f (int y)
{
struct s *ptr;
ptr = malloc (size of (struct)+99*size of (int));
}
struct s
{
int i;
float p;
};
when free (ptr) is executed, what will happen?
15. . Using pointer, changing A to B and B to A is swapping the function using two addresses and one temporary variable. a, b are address, t is temporary variable. How function look like?
16. Write a c program to find whether a stack is progressing in forward or reverse direction.
17. What is the output of the following program?
void main ()
{
char *ptr1="DCM0DAT\0ASYSTEMS";
clrscr ();
printf ("%d %s ", strlen (ptr1),ptr1);
printf ("%d %s ",strlen (ptr1+8),ptr1+8);
getch ();
}
18. How do you declare an array of N pointers to functions returning pointers to functions returning pointers to characters?
19. void main ()
{
char *s []={“dharma", "hewlett-packard", "siemens", "ibm"};
char **p;
p=s;
printf ("%s", ++*p);
printf ("%s", *p++);
printf ("%s", ++*p);
}
20. Which holds true for the following statement
class c: public A, public B
a) 2 member in class A, B should not have same name
b) 2 member in class A, C should not have same name
c) both
d) none
21. Given the following c program
func (int *i, int*j)
{*i=*i * *i;
*j=*j* *j;
}
main ()
{ int i = 5, j = 2;
func (&i, &j);
printf ("%d %d", i, j);
}
What is the output?
22. For the following C program
void insert (key, r)
type key key, data array r;
{extern int n;
if (n>=max) /*error table if full */
else r [n++]. k=key;
}
This on executing, enables a
a) Basic sequential search b) Binary search c) Interpolation search d) None
23. f (char *p)
{
p [0]? f (++p): 1;
printf ("%c", *p);
}
if call that function with f (Aabcd) what is the output??
24 In a class only declaration of the function is there but definition is not there then what is that function ?
25 What is the use of global static variable in C?
#######################################################################
2. ANSWERS
#######################################################################
1 (b)
15 swap (int *, int *, int)
18 The first part of this question can be answered in at least three ways:
a). char *(*(*a [N]) ())();
b) Build the declaration up incrementally, using typedefs:
typedef char *pc; /* pointer to char */
typedef pc fpc (); /* function returning pointer to char */
typedef fpc *pfpc; /* pointer to above */
typedef pfpc fpfpc (); /* function returning... */
typedef fpfpc *pfpfpc; /* pointer to... */
pfpfpc a [N]; /* array of... */
c) Use the cdecl program, which turns English into C and vice versa:
cdecl> declare a as array of pointer to function returning pointer to function returning pointer to char
char *(*(*a []) ())()
cdecl can also explain complicated declarations, help with casts, and indicate which set of parentheses the arguments go in (for complicated function definitions, like the one above).
Any good book on C should explain how to read these complicated C declarations "inside out" to understand them ("declaration mimics use").
The pointer-to-function declarations in the examples above have not included parameter type information. When the parameters have complicated types, declarations can *really* get messy. (Modern versions of cdecl can help here, too.)
19. "harma" (p->add (dharma) && (*p)->harma)
"harma" (after printing, p->add (hewlett-packard) &&(*p)->harma)
"ewlett-packard"
20. (a)
23. dcbaA (Just reversing the string)
24. virtual function
Subscribe to:
Posts (Atom)
Popular Posts
-
download AVG AntiVirus FREE 2014 Free Download AVG 2013 PC Activation Code - Serial Keys - License Keys - Product Keys - Seri...
-
AVAST has acquired Jumpshot to provide you with a cleaner, faster PC experience. For 25 years, you've trusted us to protect your P...