C++ Questions

Q:

What is the difference between stack and array?

Answer

Stack:


 - Stack is a ordered collection of items


 - Stack is a dynamic object whose size is constantly changing as items are pushed and popped .


 - Stack may contain different data types


Array:


- Array is an ordered collection of items


- Array is a static object i.e. no of item is fixed and is assigned by the declaration of the array


- It contains same data types.

Report Error

View answer Workspace Report Error Discuss

Subject: C++ Exam Prep: GATE

24 15561
Q:

What is the output of this program?

#include
        using namespace std;
        struct sec {
            int a;
            char b;
        };
        int main()
        {
            struct sec s ={25,50};
            struct sec *ps =(struct sec *)&s;
            cout << ps->a << ps->b;
            return 0;
        }

A) 252 B) 253
C) 254 D) 262
 
Answer & Explanation Answer: A) 252

Explanation:

In this program, We are dividing the values of a and b, printing it.

Report Error

View Answer Report Error Discuss

Filed Under: C++

0 15472
Q:

What is the output of this program?

#include
        using namespace std;
        void fun(int x, int y)
        {
            x = 20;
            y = 10;
        }
        int main()
        {
            int x = 10;
            fun(x, x);
            cout << x;
            return 0;
        }

A) 10 B) 20
C) compile time error D) none of these
 
Answer & Explanation Answer: A) 10

Explanation:

In this program, we called by value so the value will not be changed, So the output is 10

Report Error

View Answer Report Error Discuss

Filed Under: C++

0 14868
Q:

What is the output of this program?

#include
        using namespace std;
        struct sec {
            int a;
            char b;
        };
        int main()
        {
            struct sec s ={25,50};
            struct sec *ps =(struct sec *)&s;
            cout << ps->a << ps->b;
            return 0;
        }

A) 252 B) 253
C) 254 D) 262
 
Answer & Explanation Answer: A) 252

Explanation:

In this program, We are dividing the values of a and b, printing it.

Report Error

View Answer Report Error Discuss

Filed Under: C++

1 13960
Q:

What is the output of this program?

 #include
       using namespace std;
       int main()
       {
           int arr[] = {4, 5, 6, 7};
           int *p = (arr + 1);
           cout << arr;
           return 0;
       }

A) 4 B) 5
C) address of arr D) 7
 
Answer & Explanation Answer: C) address of arr

Explanation:

As we couted to print only arr, it will print the address of the array.

Report Error

View Answer Report Error Discuss

Filed Under: C++

0 13786
Q:

Explain static and dynamic memory allocation with an example each.

Answer

When amount of memory to be allocated is known beforehand i.e. at the the time of compilation, it is known as Static Memory Allocation. Once the memory is allocated statically, it cannot be deallocated during program run. So it leads to wastage of storage space.


Example:


int A[100];


 


When amount of memory to be allocated is not known beforehand, rather it is determined at the time of program run, it is called Dynamic Memory Allocation. It leads to efficient utilization of storage space.


Example:


cout << " Enter number of elements: ";


cin >> N;


int *A = new int[N]; // dynamic memory allocation

Report Error

View answer Workspace Report Error Discuss

Subject: C++

13 13092
Q:

What is the general syntax for accessing the namespace variable?

A) namespaceid::operator B) namespace,operator
C) namespace#operator D) none of the mentioned
 
Answer & Explanation Answer: A) namespaceid::operator

Explanation:
Report Error

View Answer Report Error Discuss

Filed Under: C++

1 12036
Q:

Which is also called as abstract class?

A) virtual function B) pure virtual function
C) derived class D) None of these
 
Answer & Explanation Answer: B) pure virtual function

Explanation:

Classes that contain at least one pure virtual function are called as abstract base classes.

Report Error

View Answer Report Error Discuss

Filed Under: C++

8 11034