以下接连几篇文章来自于:http://www.acetheinterview.com,本人只是做了搜集整理工作。希望对大家有用。
C/C++ Questions & Answers (1’ ~ 21’)
1、What is polymorphism?
'Polymorphism' is an object oriented term. Polymorphism may be defined as the ability of related objects to respond to the same message with different, but appropriate actions. In other words, polymorphism means taking more than one form.
Polymorphism leads to two important aspects in Object Oriented terminology - Function Overloading and Function Overriding.
Overloading is the practice of supplying more than one definition for a given function name in the same scope. The compiler is left to pick the appropriate version of the function or operator based on the arguments with which it is called.
Overriding refers to the modifications made in the sub class to the inherited methods from the base class to change their behaviour.
2、What is operator overloading?
When an operator is overloaded, it takes on an additional meaning relative to a certain class. But it can still retain all of its old meanings.
Examples:
1) The operators >> and << may be used for I/O operations because in the <iostream> header, they are overloaded.
2) In a stack class it is possible to overload the + operattor so that it appends the contents of one stack to the contents of another. But the + operator still retains its original meaning relative to other types of data.
Also Polymorphism can be achieved in C++ through operator overloading
3、Declare a void pointer.
I think the answer is simply
void* p;
malloc is just the library function called to allocated some memory and of course a void pointer will be returned , but it is the declaration of a void pointer.
4、What are templates?
C++ Templates allow u to generate families of functions or classes that can operate on a variety of different data types, freeing you from the need to create a separate function or class for each type. Using templates, u have the convenience of writing a single generic function or class definition, which the compiler automatically translates into a specific version of the function or class, for each of the different data types that your program actually uses.
Many data structures and algorithms can be defined independently of the type of data they work with. You can increase the amount of shared code by separating data-dependent portions from data-independent portions, and templates were introduced to help you do that.
5、Type-define a function pointer which takes a int and float as parameter and returns a float *.
the pointer to function can be type defined as:
typedef float*(*pf)(int a, float b) tagPF;
6、What does the following C statement do?
while(*c++ = *d++); assuming c and d are pointers to characters.
String copy is performed indeed but be careful with the space allocated for the destination string.
Check this example:
char s1[10]="abcde";
char s2[3];
char* c,*d;
c=s2;
d=s1;
while(*c++ = *d++);
printf("%s - %s\n",s1,s2);
The code is string copy. But it does not add a null pointer to the end(*). There should also be a check for overlapping addresses(O).
7、How do you call a C module within a C++ module.
You should use extern "C" for functions, compiled by C compiler and called within a C++ class. You should do that to force the linker to resolve the function name (precisely, the mangling of the name) correctly.
8、What is the difference between run time binding and compile time binding? Discuss.
Compile Time Binding : In case of operator overloading and function overloading the name of the function is resolved during the compile time . Even if there are two or more functions with the same name the compiler mangles the name so that each function is uniquely identified . This has to be resolved at compile time and is known as compile-time binding or early binding.
Run Time Binding : In case of polymorphism (virtual functions) if a base class pointer(or reference) is allocated a pointer(or reference) of derived class the actual function called is determined only during runtime through the virtual table entry . This is runtime binding or late binding
9、Compare and contrast C++ and Java.
1>Platform Independent : Java code is said to be a multiplatform code and can run on any platform because after the compilation of the source code byte code(s) are created rather than a binary code so it can run on any platform which supports JVM concept but on the contrast at time(s) it slows down the application tremendously
2> Garbage Collection : Java handles freeing up of the memory but this is not guranteed since the GC thread has the lowest priority
3>Operator Overloading : is not provided in Java,but what are the advantages of Operator Overloading but one may question what are its advantages, well it makes a more readable and a modular code. In c++ cin and cout objects can also be overloaded which again leads to a better readability and flexibility
4> Multiple Inheritance : Java does provide multiple inheritance in form of Interfaces, In Java a class can not inherit from more than one class but it definitely can implement any number of interfaces
5> Templates: in c++ give such a lot of flexibility and avoids redundant coding which again is not provided by Java
10、Why does C/C++ give better run-time performance then Java?
That's because the Java bytecode is interpreted, not compiled. Programs
written in C are compiled into binaries which can be executed by a specific computer processor. Programs written in Java require one more step -- they must be interpreted by the Java "virtual machine" before running on a particular computer architecture. As a result, a computer running a Java program has to execute more machine-language instructions to do the same amount of work than a computer running an equivalent program written in C.
11、Does C++ come with in-built threading support.
No. C++ does not support in-built Multithreading. To do so, you must use the operating system functions manually. (Better use pthread library.)
12、Class A derived B derived C. All have foo(). I cast C to A and call foo(). What happens?(*)
--foo() for class C will be called.
--it depends. if in A foo is defined as virtual function. then call C's foo(), if it doesn't defined virtual, then call A's foo()
--Actually, if access is NOT specified, it deafults to private derivation. In private derivation, binding is static. So, whether foo is declared virtual or not it still defaults to static binding. So, A->foo() is called. However, If a public derivation is specified from C <-- B and B <-- A, then if foo() is virtual C->foo() is called; if foo() is non virtual A->foo() is called.
--But I think there is an important thing neglected by all of you, that is ‘cast C to A’, NOT ‘cast C* to A*’, So the correct answer is A.foo() will be called.
13、All classes A, B, C have default constructor, foo() that calls parent foo() and allocates 100 bytes to their own private local variable, and a destructor that frees the 100 bytes. I create a C object and then destroy it. What's the problem? Did all the memory get freed? What if I create C, cast to A, and then destroy it. How would I make sure memory is freed?
--destructor must be "virtual" and each destructor must call parent destructor
14、What errors are caught at compile time vs link time?
Syntactical and symantical errors (code errors) are caught at compile time.
Dependency errors (for example resolving function calls or errors in including other modules) are caught at link time.
15、What is the value of "a" after this?
int (*a) [10];
a++;
int (*a)[10]; represents the declaration of a pointer to an array of ten integers. So the value of a is initially some address allocated by the compiler and don't rely on the fact the address is 0 as it happens for static variables.
The value can be zero if you add the "static" keyword in front of declaration but I don't advise you to further use this pointer to access some elements.
If the integer is n bytes ( 2 or 4 depending on the language) it is true that the value of a will be increase with 10*n.
Test this program to understand:
#include <stdio.h>
void main(int argc,char*argv[])
{
int b[10]={1,2,3,4};
int (*a)[10];
printf("%p\n", a); // invalid address!!
printf("%d\n",(*a)[0]); // runtime error!!
a++; // advance 10 * sizeof(int) bytes
printf("%p\n", a);
a=&b;
printf("%p\n", a);
printf("%d\n",(*a)[0]);
}
16、What is wrong with this?
main(){
int *ptr;
*ptr=10;
}
Actual reality is like this. When the pointer is declared it contains some garbage value as u know. Conceptually speaking it points to location indicated by that "garbage memory address". Now when we assign some value to *a ; i.e.
*a=12;
we are trying to place 12 in the memory cell number indicated by 'a' ; which in this case is any random memory address because it is garbage. So the program will be compiled fine. But when run ; if the garbage value assigned to 'a' is invalid (or restricted by OS) memory address ; the OS will generate error. So its upto OS.
But technically program is fine.
-Dhiraj
17、Given int n, i=10, j=20, x=3, y = 100;
What is the value of n and y at the end of each of the following expressions?
a) n = (i > j) && (x < ++y);
b) n = (j - i) && (x < y++);
c) n = (i < j)
1> n = 0, y = 100, second condition will not be evaluated.
2> n = 1, y = 101
3> n = 1, y = 100
18、int x = 5;
int y = 7;
What is the value of x and y after the expression y+=x++;
Y = 12 and X = 6
Why? because X will be incremented after y = Y+X has been carried out and result has been assigned to Y.
if it would have been y+=++x then the value of y would have been equal to 13 and x = 6
I am sure about this
Mohit
19、What's the difference between C and C++?
C is Top Down Approach
C++ is Bottom Up Programming approach.
C does not support OOP (Object Oriented Programming) and do not support PolyMorphism, Inheritence, Encapsulation, Function OverLoading.
There is an important difference but often neglected by many people is the error handling.
Some common C style commands and there corresponding C++ style commands are shown below.
Console I/O
***********
C
===
printf("Hello World!\n");
scanf("%s", name);
C++
===
cout << "Hello World!" << endl;
cin >> name;
Comments
********
C
===
/* comment */
C++
===
// comment
File extensions
***************
C
===
.c, .h
C++
===
.C, .h, .CPP, .HPP
File I/O
*********
C
===
out = fopen("output_file.dat", "wb");
in = fopen("input_file.dat", "rb");
C++
===
ofstream out("output_file.dat");
ifstream in("input_file.dat");
Dynamic Memory
**************
C
===
text = (char *) malloc(1000);
free(text);
C++
===
text = new char[1000];
delete [] text;
Constants
*********
C
===
#define PI 3.14159
C++
===
const float PI = 3.14159;
Macros
******
C
===
#define MAX(a,b) ((a) > (b) ? (a) : (b))
C++
===
inline int MAX(int a, int b) { return a > b ? a : b; }
20、What does Public and Private mean in C++
--Public:
Makes class memebrs accessible outside the class. It can be accessed in the C code.
Private:
Makes the members specified accessible only to the class and it's functions.
--public / private have two different meanings in C++:
1. To specify the access privilege of the class members.
2. To specify what type of inheritance.
21、Is it possible to keep 2 stacks in a single array, if one grows from position one of the array, and the other grows from the last position. Write a procedure PUSH(x,s) that pushes element x onto stack S, where S is one or the other of these two stacks. Include all necessary error checks (Sent by Puneet Saraf)
The question did not ask for a suggestion. It is definitely possible to implement 2 stacks using a single array. The array shud be visible to both stacks (and therefore be global, or shud provide equivalent effect). Also the stack tops shud be taken care of. one top will decrement everytime the push method is called and one would be incremented. As soon as s1.top==s2.top we shud throw an exception.本文地址:http://com.8s8s.com/it/it25983.htm