热门面试题与答案和在线测试
面向面试准备、在线测试、教程与实战练习的学习平台

通过聚焦学习路径、模拟测试和面试实战内容持续提升技能。

WithoutBook 将分主题面试题、在线练习测试、教程和对比指南整合到一个响应式学习空间中。

面试准备

模拟考试

设为首页

收藏此页面

订阅邮箱地址

C++ 面试题与答案

Test your skills through the online practice test: C++ Quiz Online Practice Test

相关差异对比

C vs C++Java vs C++

问题 81. Will the following program execute?
void main()
{
void *vptr = (void *) malloc(sizeof(void));
vptr++;
}

Answer1
It will throw an error, as arithmetic operations cannot be performed on void pointers.

Answer2
It will not build as sizeof cannot be applied to void* ( error ?Unknown size? )

Answer3
How can it execute if it won?t even compile? It needs to be int main, not void main. Also, cannot increment a void *.

Answer4
According to gcc compiler it won?t show any error, simply it executes. but in general we can?t do arthematic operation on void, and gives size of void as 1

Answer5
The program compiles in GNU C while giving a warning for ?void main?. The program runs without a crash. sizeof(void) is ?1? hence when vptr++, the address is incremented by 1.

Answer6
Regarding arguments about GCC, be aware that this is a C++ question, not C. So gcc will compile and execute, g++ cannot. g++ complains that the return type cannot be void and the argument of sizeof() cannot be void. It also reports that ISO C++ forbids incrementing a pointer of type ?void*?.

Answer7
in C++
voidp.c: In function `int main()?:
voidp.c:4: error: invalid application of `sizeof? to a void type
voidp.c:4: error: `malloc? undeclared (first use this function)
voidp.c:4: error: (Each undeclared identifier is reported only once for each function it appears in.)
voidp.c:6: error: ISO C++ forbids incrementing a pointer of type `void*?

But in c, it work without problems

void main()
{
char *cptr = 0?2000;
long *lptr = 0?2000;
cptr++;
lptr++;
printf(? %x %x?, cptr, lptr);
}
Will it execute or not?


Answer1
For Q2: As above, won?t compile because main must return int. Also, 02000 cannot be implicitly converted to a pointer (I assume you meant 02000 and not 0?2000.)

Answer2
Not Excute.
Compile with VC7 results following errors:
error C2440: ?initializing? : cannot convert from ?int? to ?char *?
error C2440: ?initializing? : cannot convert from ?int? to ?long *?


Not Excute if it is C++, but Excute in C.
The printout:
2001 2004

Answer3
In C++
[$]> g++ point.c
point.c: In function `int main()?:
point.c:4: error: invalid conversion from `int? to `char*?
point.c:5: error: invalid conversion from `int? to `long int*?

in C
????????????
[$] etc > gcc point.c
point.c: In function `main?:
point.c:4: warning: initialization makes pointer from integer without a cast
point.c:5: warning: initialization makes pointer from integer without a cast
[$] etc > ./a.exe
2001 2004

这有帮助吗? 添加评论 查看评论
 

问题 82. What is the difference between Mutex and Binary semaphore?

semaphore is used to synchronize processes. where as mutex is used to provide synchronization between threads running in the same process. 

这有帮助吗? 添加评论 查看评论
 

问题 83. In C++, what is the difference between method overloading and method overriding?


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 overriding is the ability of the inherited class rewriting the virtual method of the base class.

这有帮助吗? 添加评论 查看评论
 

问题 84. What methods can be overridden in Java?


In C++ terminology, all public methods in Java are virtual. Therefore, all Java methods can be overwritten in subclasses except those that are declared final, static, and private.

这有帮助吗? 添加评论 查看评论
 

问题 85. What are the defining traits of an object-oriented language?


The defining traits of an object-oriented langauge are:
* encapsulation
* inheritance
* polymorphism

这有帮助吗? 添加评论 查看评论
 

void main()
{
void *vptr = (void *) malloc(sizeof(void));
vptr++;
}

Answer1
It will throw an error, as arithmetic operations cannot be performed on void pointers.

Answer2
It will not build as sizeof cannot be applied to void* ( error ?Unknown size? )

Answer3
How can it execute if it won?t even compile? It needs to be int main, not void main. Also, cannot increment a void *.

Answer4
According to gcc compiler it won?t show any error, simply it executes. but in general we can?t do arthematic operation on void, and gives size of void as 1

Answer5
The program compiles in GNU C while giving a warning for ?void main?. The program runs without a crash. sizeof(void) is ?1? hence when vptr++, the address is incremented by 1.

Answer6
Regarding arguments about GCC, be aware that this is a C++ question, not C. So gcc will compile and execute, g++ cannot. g++ complains that the return type cannot be void and the argument of sizeof() cannot be void. It also reports that ISO C++ forbids incrementing a pointer of type ?void*?.

Answer7
in C++
voidp.c: In function `int main()?:
voidp.c:4: error: invalid application of `sizeof? to a void type
voidp.c:4: error: `malloc? undeclared (first use this function)
voidp.c:4: error: (Each undeclared identifier is reported only once for each function it appears in.)
voidp.c:6: error: ISO C++ forbids incrementing a pointer of type `void*?

But in c, it work without problems

void main()
{
char *cptr = 0?2000;
long *lptr = 0?2000;
cptr++;
lptr++;
printf(? %x %x?, cptr, lptr);
}
Will it execute or not?
2) What is the difference between Mutex and Binary semaphore?
3) In C++, what is the difference between method overloading and method overriding? 4) What methods can be overridden in Java? 5) What are the defining traits of an object-oriented language? " />

用户评价最有帮助的内容:

版权所有 © 2026,WithoutBook。