c c and data structure questions

6
Easy Questions C& C++ 1. By default real variable is treated as which type of variable? A: Double 2. Switch is more efficient or if-else? A: Both are almost equal (if conditions are more) Switch is clearer and good way of programming 3. If I don't specify either public or private sections in a class, what is the default? A. In a class, all members are private by default if neither public or private sections are declared. 4. What's the difference between the keywords STRUCT and CLASS? A: The members of a STRUCT are PUBLIC by default, while in CLASS, they default to PRIVATE. They are otherwise functionally equivalent. 5. What is declaration and definition of variable? Declaration: It is giving the type of variable or just creating a variable Ex: int a; Definition: Here space is allocated to the variable and its value isinitialized A=5; 6. What is a friend member function? A. Declaring a friend gives non-members of a class (i.e., other classes) access to the non- public members (i.e. private) of a class. 7. What is the "this" pointer? A. "this" is a local variable in the body of a non-static member function. It is a pointer to the object for which the function was invoked. It cannot be used outside of a class member function body. 8. What's the deal with operator overloading? Operator overloading allows C/C++ operators to have user-defined meanings on user-defined types (classes). 9. What operators can/cannot be overloaded? The only C operators that can't be are.(dot operator) and ?:(conditional operator) (and sizeof, which is technically an operator). C++ adds a few of its own operators, most of which can be overloaded except :: (scope resolution) and - >(reference operator). 10. What is Namespace? A: It is a feature in C++ to minimize name collisions in the global name space. This namespace keyword assigns a distinct name to a library that allows other libraries to use the same identifier names without creating any name collisions. Furthermore, the compiler uses the namespace signature for differentiating the definitions. 11. Explain "passing by value" and "passing by reference" A: Passing by value makes a copy of the variable and can be quite slow and inefficient if the variable is large as a array.

Upload: chellaanilkumar

Post on 12-Nov-2014

362 views

Category:

Documents


3 download

TRANSCRIPT

Page 1: C C and Data Structure Questions

Easy QuestionsC& C++

1. By default real variable is treated as which type of variable?A: Double

2. Switch is more efficient or if-else?A: Both are almost equal (if conditions are more)Switch is clearer and good way of programming

3. If I don't specify either public or private sections in a class, what is the default?A. In a class, all members are private by default if neither public or private sections are declared.

4. What's the difference between the keywords STRUCT and CLASS?A: The members of a STRUCT are PUBLIC by default, while in CLASS, they default toPRIVATE. They are otherwise functionally equivalent.5. What is declaration and definition of variable?

Declaration: It is giving the type of variable or just creating a variableEx: int a;Definition: Here space is allocated to the variable and its value isinitializedA=5;

6. What is a friend member function?A. Declaring a friend gives non-members of a class (i.e., other classes) access to the non-public members (i.e. private) of a class.7. What is the "this" pointer?

A. "this" is a local variable in the body of a non-static member function. It is a pointer to theobject for which the function was invoked. It cannot be used outside of a class memberfunction body.8. What's the deal with operator overloading?

Operator overloading allows C/C++ operators to have user-defined meanings on user-definedtypes (classes).9. What operators can/cannot be overloaded?

The only C operators that can't be are.(dot operator) and ?:(conditionaloperator) (and sizeof, which is technically an operator). C++ adds a few of its ownoperators, most of which can be overloaded except :: (scope resolution) and ->(reference operator).10. What is Namespace?A: It is a feature in C++ to minimize name collisions in the global name space. This namespacekeyword assigns a distinct name to a library that allows other libraries to use the sameidentifier names without creating any name collisions. Furthermore, the compiler uses thenamespace signature for differentiating the definitions.11. Explain "passing by value" and "passing by reference"

A:Passing by valuemakes a copy of the variable and can be quite slow and inefficient if thevariable is large as a array.

Page 2: C C and Data Structure Questions

Pass by Reference passes in a reference to a variable - this is effectively the address ofthe variable into the function. This is considerably more efficient than by value andallows the function to change the variable directly.

12. What is a NULL Pointer? Whether it is same as an uninitialized pointer?A:A null pointer points nowhere; it is not the address of any object or function.A null pointer is conceptually different from an uninitialized pointer. A null pointer isknown not to point to any object or function; an uninitialized pointer might pointanywhere.

13. What is the use of typedef?A: typedef is a keyword in the C and C++ programming languages. It is used to give adata type a new name.

14. When an object created and what is its lifetime?A:When an class is instantiated the object is created and its lifetime is till the end of theexecution of program in normal conditions (free and destructor can be called to delete theobject)

15. When will a constructor executed?A: When class is instantiated with new keyword the Constructer is called.

16. Different types of polymorphism in c++ Polymorphism?A:C++ supports two kinds of static (compile-time) and dynamic (run-time) polymorphism.Compile-time polymorphism does not allow for certain run-time decisions, while run-time polymorphism typically incurs a performance penalty.

17. What are the techniques you use for debugging?A: F8 key is used for debugging and F7 is used for tracingSometimes printf can be used to print and check the values

18. What is the difference between "overloading" and "overriding"?A:Overloading - Two functions having same name and return type, but with different typeand/or number of arguments.Overriding - When a function of base class is re-defined in the derived class.

19. What is a dangling pointer?A:

The object or variable to which the pointer is referencing is deleted then the pointer is pointingto novalid value. So, it is called Dangling pointer.

Def. A dangling pointer arises when you use the address of an object after its lifetime is over.20. When can you tell that a memory leak will occur?

A:When a program exits but the variable (mostly the dynamically allocated) space is not deleted andthe space allocated becomes unusable then it is called as Memory LeakDef.A memory leak occurs when a program loses the ability to free a block of dynamicallyallocated memory.

Page 3: C C and Data Structure Questions

21. Differentiate between a deep copy and a shallow copy?A:Deep copy involves using the contents of one object to create another instance of the same class.In a deep copy, the two objects may contain the same information but the target object will haveits own buffers and resources. The destruction of either object will not affect the remaining object.The overloaded assignment operator would create a deep copy of objects.Shallow copy involves copying the contents of one object into another instance of the same classthus creating a mirror image. Owing to straight copying of references and pointers, the two objectswill share the same externally contained contents of the other object to be unpredictable.

22. Describe the main characteristics of static functions.A:The main characteristics of static functions include,

• It is without the a this pointer,• It can't directly access the non-static members of its class• It can't be declared const, volatile or virtual.• It doesn't need to be invoked through an object of its class, although for convenience, it

may.

23. What does abstract data type means?A: An ADT is a formally specified set of values and associated operations,independent from any particular implementation. ADTs are one of thefoundations of OO, because they formalize the notion of "informationhiding" or "encapsulation", which hides the implementation of an ADT(i.e., a class) behind its interface.

24. DefineSyntax ErrorLogical ErrorRuntime ErrorA:Syntax Error-Errors in coding which arise because syntax is not followed properly. It canoccur by human mistake in typing & dueLack knowledge of language.

EX - Ram are a boy. ErrorCorrect- Ram is a boy.

Logical Error- Here Syntax is correct; program will executeproperly but willnot givecorrectanswer.

EX- Table is boy syntax correctBut meaning is not

Page 4: C C and Data Structure Questions

Runtime Error- overflow, underflow, out of memory capacity.The exceptions like divide a number by 0

A bit Tricky Questions25. What is RTTI?

In programming, RTTI (Run-Time Type Information or Run-Time Type Identification)refers to a C++ system that keeps information about an object's data type in memory atruntime. Run-time type information can apply to simple data types, such as integers andcharacters, or to generic objects. This is a C++ implementation of a more generic conceptcalled reflection.

26. Why preincrement operator is faster than post increment?A: Post increment is usually less efficient than preincrement because it has to remember& return its original value

27. Explain working of printf.A: Printf is a Variadic function. Variadic functions arefunctions which may take a variable number of arguments andare declared with an ellipsis in place of the last parameter.int printf(char *, ...);Uses Var_args to know the variables in the calling functionIn C these are defined in stdarg.h

Programs28. What is output

Float a=0.7if(0.7>a)printf(“Hello”);elseprintf(“Hi”);

A: Hello

Another variation of same question will beif(0.7==a)printf(“Hello”);elseprintf(“Hi”);

A: Hi

29. What is outputint i=2printf(“%d”,++i,++i);

A: Error order of evaluation not specified

Page 5: C C and Data Structure Questions

30. What is outputchar str[25]=”MAQ Software”;printf(“%s”, &str+2);

A: Garbage Value

31. What is the outputenumstatus{pass, fail}enum status student1;student1=fail;printf(“%d”,student1);

A: 132. What is out put

main(){char s1[]="hello";char s2[]="world";s1=s2;printf("%s",s1);}

Compilation error giving it cannot be a modifiable 'lvalue'Data structures

33. Explain about linked list?A: A linked list is a data structure that consists of a sequence of data records such that ineach record there is a field that contains a reference (i.e., a link) to the next record in thesequence.

34. Circular Linked ListWhen the last node of liner linked list points to the first node of the list; in that case the listis said to be circular or circularly linked

35. Doubly Linked ListIn a doubly-linked list, each node contains, besides the next-node link, a second link fieldpointing to the previous node in the sequence. The two links may be called forward(s) andbackwards. Linked lists that lack such pointers are said to be simply linked, or simple linkedlists.

Page 6: C C and Data Structure Questions

36. What are the various kinds of sorting techniques? Which is has bestcase?

A: Bubble sort Quick sort Insertion sort Selection sort Merge sort Heapsort among the sorting algorithms quick sort is the best one

37. Of the following tree structure, which is, efficient considering space and timecomplexities?

a. Incomplete Binary Treeb. Complete Binary Treec. Full Binary Tree

(b) Complete Binary Tree.38. What is a spanning Tree?

A spanning tree is a tree associated with a network. All the nodes of the graph appearon the tree once.

A minimum spanning tree is a spanning tree organized so that the total edge weightbetween nodes is minimized.39. Which is the simplest file structure?

a. Sequentialb. Indexedc. Random

(a) Sequential40. If you are using C language to implement the heterogeneous linked list, what pointer type

will you use?The heterogeneous linked list contains different data types in its nodes and we need a link,pointer to connect them. It is not possible to use ordinary pointers for this. So we go for voidpointer. Void pointer is capable of storing pointer to any type as it is a generic pointer type.41. What is the data structures used to perform recursion?Stack. Because of its LIFO (Last In First Out) property it remembers its ‘caller’ so knowswhom to return when the function has to return. Recursion makes use of system stack forstoring the return addresses of the function calls.42. In tree construction which is the suitable efficient data structure?

(a) Array (b) Linked list (c) Stack (d) Queue (e) none(b) Linked list

43. In RDBMS, what is the efficient data structure used in the internal storage representation?B+ tree. Because in B+ tree, all the data is stored only in leaf nodes, that makes

searching easier. This corresponds to the records that shall be stored in leaf nodes.