分享
 
 
 

面试时最经常被问到的问题(Frenquently asked interview questions)之C/C++篇

王朝c/c++·作者佚名  2006-01-09
窄屏简体版  字體: |||超大  

以下接连几篇文章来自于: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.

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有