📜  C++程序的输出|套装36

📅  最后修改于: 2021-05-30 13:24:03             🧑  作者: Mango

问题1 。假设int的大小为4个字节,以下内容的输出是什么?

#include 
using namespace std;
  
class abc {
    void f();
    void g();
    int x;
};
  
main()
{
    cout << sizeof(abc) << endl;
}

选项 :
A)12
B)4
C)8
D)编译错误

Answer : B

解释 :
只有类成员变量才构成类或其对象的大小,因此在这种情况下,我们必须将void和一个int值组合在一起,因此类的总大小将为0 + 0 + 4(将int设为4字节)。

问题2 。以下内容的输出是什么

#include 
using namespace std;
  
int main()
{
    int a[] = { 1, 2 }, *p = a;
    cout << p[1];
}

选项 :
A)1
B)2
C)编译错误
D)运行时错误

Answer : B

解释 :
当我们将数组“ a”分配给指针“ p”时,它将保存数组的基地址。我们可以像使用“ a”一样使用“ p”访问数组


问题3
。以下内容的输出是什么

#include 
using namespace std;
  
int main()
{
    int a[] = { 10, 20, 30 };
    cout << *a + 1;
}

选项 :
A)10
B)20
C)11
D)21

Answer : C

解释 :
* a代表10,加上1则得出11。


问题4
。以下内容的输出是什么

#include 
using namespace std;
  
class Box {
    double width;
  
public:
    friend void printWidth(Box box);
    void setWidth(double wid);
};
void Box::setWidth(double wid)
{
    width = wid;
}
void printWidth(Box box)
{
    box.width = box.width * 2;
    cout << "Width of box : " << box.width << endl;
}
int main()
{
    Box box;
    box.setWidth(10.0);
    printWidth(box);
    return 0;
}

选项 :
A)40
B)5
C)10
D)20

Answer : D

解释 :
我们使用朋友函数是因为我们要打印盒子宽度的值。这是类的私有成员函数,我们无法在类外部访问私有成员。
setWidth(10.0)将width设置为10,printWidth(box)打印(宽度* 2),即20。

问题5。以下内容的输出是什么

#include 
using namespace std;
struct a {
    int count;
};
struct b {
    int* value;
};
struct c : public a, public b {
};
int main()
{
    c* p = new c;
    p->value = 0;
    cout << "Inherited";
    return 0;
}

选项 :
A)继承
B)错误
C)运行时错误
D)没有提到

Answer : A

说明:类或结构“ c”继承类“ a”以及类“ b”。当我们创建类c的对象p时,a和b都被自动继承,这意味着我们可以访问类a和b的属性。
因此,将p-> value设置为问题中给定的0,然后打印继承的代码并退出代码。

要从最佳影片策划和实践问题去学习,检查了C++基础课程为基础,以先进的C++和C++ STL课程基础加上STL。要完成从学习语言到DS Algo等的更多准备工作,请参阅“完整面试准备课程”