Wednesday

C & C++ Questions with Answers

##################################################
1. QUESTIONS
##################################################

1. * In C++, what is the difference between method overloading and method overwriting?
2. * In C, why is the void pointer useful?
3. What are the differences between a C++ struct and C++ class?
4. How many ways are there to initialize an int with a constant?

5. What is the difference between a copy constructor and an overloaded assignment operator?

6. How do I declare an array of N pointers to functions returning pointers to functions returning pointers to characters?

7. What's wrong with "char *p; *p = malloc (10);"?

8. Reversing a linked list. Given a linked list, reverse it. Input must be read from a file, "list.dat". It will just be a list of integers. The total number is not known. The program should create a linked list with the given numbers in the same order. Each node contains the value and a pointer to the next node of the list. Defining just TWO additional node pointers you must inverse the given list and print out the numbers in the list. NO OTHER VARIABLE of any type should be defined. Hence the output will be reverse of the input. e.g. Input file reads, 3 1 4 2 Linked list will be, Head -> 3 -> 1 -> 4 -> 2 -> Null the reversed list must be, Null <- 3 <- 1 <- 4 <- 2 <- Head So the output will be, 2,4,1,3. 9. main () { int i; fork (); fork (); printf ("----"); fork (); printf ("----"); } How many times the printf will be executed? 10. Give the output of the following program main () {char *s; s="hot java"; strcpy (s,"solarrs java") }

11. Give the output of the following program
main ()
{printf ("hot java");
fork ()
exit (0);
}
(i). When redirected to a screen what will be printed.
(ii). When redirected to file what will be printed.

12. Find the error in the following program
struct point
{struct point *next;
int data;
}
x;
main ()
{int i;
for (x=p;x!=0;)
x=x->next,x++;
freelist (x);
}
freelist (x)
{free (x);
return
}


13. When does a stack member will be initialized?
(a) When object is created (b) when object is initialized.
(c) doesn’t depend on object. (d) None.

14. Point out the error, if any, in the following program
main ()
{
int a=10, b;
a>= 5? b=100: b=200;
printf ("\n %d", b);
}

15. What is the difference between the following declarations?
const char *s;
char const *s;

16. What is the difference between the following declarations?
const char *const s; char const *const s;

17. Point out the error in the following program
main ()
{
const char *fun ();
*fun ()='A';
}
const char *fun ()
{
return "Hello";
}

18. Given a macro like #define MAX (A, B) {to return that is larger} what will MAX (i++, j), MAX (i, j++) return


19. 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

20. Find MAXIMUM of three distinct integers using a single C statement

21. Write a general swap macro in C:
- A macro, which can swap any type of data (i.e. int, char, float, struct, etc…)

22. Write a program to delete the entry from the doubly linked list without saving any of the entries of the list to the temporary variable.

##################################################
1. ANSWERS
##################################################

1. Overloading a method (or function) in C++ is the ability for functions of the same name to be defined as long as these methods have different signatures (different set of parameters). Method overwriting is the ability of the inherited class rewriting the virtual method of the base class.

2. The void pointer is useful because it is a generic pointer that any pointer can be cast into and back again without loss of information.

3. The default member and base-class access specifiers are different.

This is one of the commonly misunderstood aspects of C++. Believe it or not, many programmers think that a C++ struct is just like a C struct, while a C++ class has inheritance, access specifiers, member functions, overloaded operators, and so on. Some of them have even written books about C++. Actually, the C++ struct has all the features of the class. The only differences are that a struct defaults to public member access and public base-class inheritance, and a class defaults to the private access specifier and private base-class inheritance. Getting this question wrong does not necessarily disqualify an applicant. Getting it right is a definite plus.

4. Two.

There are two formats for initializers in C++ as shown in the example that follows. The first format uses the traditional C notation. The second format uses constructor notation.

int foo = 123;

int bar (123);

It's acceptable when a programmer does not know about the second notation, although they should certainly know about the first one. Many old-timer C programmers who made the switch to C++ never use the second idiom, although some wise heads of C++ profess to prefer it. If your applicant is quick with the right answer, that's a good sign.

5. A copy constructor constructs a new object by using the content of the argument object. An overloaded assignment operator assigns the contents of an existing object to another existing object of the same class.

6. No solution available

7. No solution available

8. Reversing a linked list. Simple. Use two new pointer p, q and the head that’s already available. Invert the first three nodes and keep iterating along the list. Else use recursion.

9-13 Answers/Solutions not available

14. lvalue required in function main (). The second assignment should be written in parenthesis as follows:
a>= 5 ? b=100: (b=200);
15. No Difference

16. No Difference

17. fun() returns to a "const char" pointer which cannot be modified

19. (a)

20. d= (a>b)? ((a>c)? a: c)) : ((b>c)? b: c))

##################################################
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, &amp;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) &&amp; (*p)->harma)
"harma" (after printing, p->add (hewlett-packard) &&(*p)->harma)
"ewlett-packard"
20. (a)
23. dcbaA (Just reversing the string)
24. virtual function


I hope this is useful._______________________

No comments:

Post a Comment

Did you like it ? Write your comment.