Explore topic-wise InterviewSolutions in .

This section includes InterviewSolutions, each offering curated multiple-choice questions to sharpen your knowledge and support exam preparation. Choose a topic below to get started.

19151.

Write a C++ structure definition to store the student details given below:1. Admission number 2. Name 3. Marks in six subjects

Answer»

Struct student 

intAdmno; 

charname[30]; 

int mark[6]; 

}

19152.

(a) Write a C++ program to store and print information (name, roll and marks) of a student using structure.(b) Write a program in C++ to input the total marks obtained by a group of students in a class and display them in descending order using pointer.

Answer»

(a) #include<iostream>

using namespace std;

int main()

{

struct student

{

char name[40];

int roll;

int marks[6];

}
S1;

count<<"Enter name";

cin>>S1.name;

cout<<"Enter roll no";

cin>>S1.roll;

cout<<"Enter 6 marks";

for (int i=0;i<6;i++)

cin>>S1.marks[i];

cout<<"The details are given below";

cout<<"\n Name:"<<S1.name;

cout<<"\n Roll No:"<<S1.roll;

for (int i=0;i<6;i++)

cout<<"\n mark"<<i + 1<<S1.marks[i];

return 0;

}

(b) #include<iostream>

using namespace std;

int main ()

{

int main [50],i,j,*ptr,n,temp, ptr = mark;

cout<<"Enter how many student";

cin>>n;

for(i=0;i<n;i++)

{

cout<<"Enter mark"<<i+1<<";";

cin>>*(ptr++)

}

for(i=0;i<n-1;i++)

for(j=i+1;j<n;j++)

if(mark[i]>mark[j])

{

temp = mark[i];

mark[i] = mark[j];

mark[j] = temp;

} ptr = mark;

cout<<"The marks in descending order";

for(i=0;j<n;i++)

cout<<*(ptr++)<<endI;

}

19153.

Run time allocation of memory is triggered by the operator.

Answer»

Run time allocation of memory is new

19154.

Complete the following structure definition, to store students details with appropriate values. Students details are roll number, name and height..... Struct students{..... mochar .......... height};

Answer»

struct students 

short rno; 

char name[25]; 

float height; 

};

19155.

Correct the following structure definition to represent an employee with employee code, employee name, and salary.

Answer»

Employee struct 

{ ecode int; 

char name[25];

salary double; 

}

19156.

What does the following stmt mean? int *P = new int [15];

Answer»

Allocating memory for an integer array of 15 elements.

19157.

Write the declaration statement of a student structure variable named ‘s’ which is initialized with admission number 19, name as ‘Aysha’ and mark as 75.

Answer»

student s = {19,“aysha”,75};

19158.

Compare the aspects of arrays and structures.

Answer»

Array-:

An array is a collection of elements with same data type Or with the same name we can store many elements, the first or second or third etc can be distinguished by using the index(subscript). The first element’s index is 0, the second elements index is 1, and so on.

To store 100 numbers the array declaration is as follows

int n[100]; By this we store 100 numbers. The index of the first element is O and the index of last element is 99.

Structure -:

But a Structure is a group of different types of Ipgically related data referenced by a single name.

Eg. The collection of details of a student that may contain various data with different data types.

Eg.

struct student

{

int adm_no;

char name[40];

float weight;

};

OR

    Structure    Array
1. It is a user defined data typePredefined data type
2. It is a collection of different types of logically related data under one name.Collection of data elements of same data type having a common name.
3. Elements referenced using dot operator(.)Elements reference using its subscripts (position value)
4. When an element of a structure becomes another structure nested structure and complex structures are formed.When an element of another becomes another array, multidimensional arrays are formed.
5. Structure contains array as its elementsArray of structure can be formed.
19159.

The contents of two pointers that point to adjacent variable of type oat differ by (a) 4 (b) 2(c) 1 (d) 8

Answer»

Correct answer is (a) 4

19160.

Observe the following layout of data,Last transaction dateAccount no.Name of account holderDayMonthYearBalance Amount1. Name the data type in C++ used to represent the above data format. 2. Write the definition for implementing the above layout.

Answer»

1. Structure 

2. struct Account 

int accno; 

char name[30]; 

struct lastdate 

int day; 

int month;

int year; 

} tdate; 

float 

balanceamt; 

}

19161.

If ‘p’ is pointer to the variable ‘x’, which of the following statement are correct. (a) ‘p’ and ‘x’ may be of different data types (b) if ‘p’ points to ‘x’, then ‘x’ also points to ‘p’ (c) the statement x = &amp;p is valid (d) p will give the value of the variable ‘x’

Answer»

(d) p will give the value of the variable ‘x’

19162.

State True of False. ‘Only integers can be added or subtracted from pointers’.

Answer»

‘Only integers can be added or subtracted from pointers’. is  True

19163.

Distinguish between int * ptr = new int (5); int * ptr = new int [5];

Answer»

The first statement allocates memory of one integer to ptr and initialized it with value 5. The second statement allocates memory of 5 contiguous integers (ie. an array of 5 integers) and stores begining address in pointer ptr.

19164.

What is meant by memory leak? What are the reasons for it? How can we avoid such a situation?

Answer»

If the memory allocated using new operator is not de allocated using delete , that memory is left unused and not released for further allocation. Such memory blocks are called orphaned memory. On each execution the amount of orphaned block is increased. This situation is called memory leak. Use delete operator to avoid this.

19165.

Read the structure definition given below and answer the following questions:struct sample{int num;char *str;} *sptr(a) Write C++ statements to dynamically allocate a location for sample type data and store its address in sptr. (b) Write C++ statements to input data into the location pointed to by sptr. (c) Modify this structure into a self-referential structure.

Answer»

(a) sptr=new sample;

(b) cin>>sptr->num;

cin>>sptr->str;

Example

#include<iostream>

using namespace std;

struct sample

{

int num;

char *str;

}*sptr;

int main()

{

sptr = new  sample;

cout <<"Enter value for num:";

cin>>sptr->num;

cout<<"Enter value for str:";

cin>>sptr->str;

cout<<sptr->num<<sptr->str;

}

(c) struct sample

{

int num;

char *str;

sample *str;

};

19166.

What does the following statement mean?

Answer»

It is accessing the element of a nested structure. S is a structure variable that contains another structure variable dob. ‘month’ is a member of inner structure variable dob. This statement assigns a value 10 to the member month.

19167.

Choose the operator which is not associated with a pointer variable. (a) * (b) . (c) &amp; (d) →

Answer»

Correct answer is (b) .

19168.

The array of character pointers is generally preferred over two dimensional array of characters. Justify.

Answer»

The array of character pointers is preferred. 

There are two reasons: 

1. An array of pointers makes more efficient use of available memory by consuming lesser number of bytes to store the strings.

2. An array of pointers makes the manipulation of the strings much easier. One can easily exchange the positions of the strings in the array of pointers without actually touching their memory locations.

19169.

Write a program to input a string and check whether it is palindrome or not using character pointer.

Answer»

#include <iostream>

using namespace std;

int main()

{

char *str="";

int len,i,j,flag=0;

cout<<"Enter a string";

cin>>str;

for(i=0;*(str+i)!="\0;i++);

len=i;

for(i=0;j=len-1;i<len/2;i++;j--)

{

if(*(str+i)!=*(str+j))

{

flag=1;

break;

}

}

if(flag==1)

cout<<*It is not palindrome";

else

cout<<"It is palindrome";

}

19170.

With an example explain how structures can be initialized.

Answer»

Student stud = {“Athul”, 17,101, “XII Science”}; The above statement initializes structure variable stud of type student which has data members name, age, roll no, batch.

19171.

Consider the following C++ statement. Are they equivalent?char ch = 67;charch = ‘C’;

Answer»

Both the statements are equivalent as they declare ‘ch’ to be char and initialize it with the value of 67. Since this is the ASCII code for ‘C’ , the character constant also can be used to initialize ‘ch’ to 67.

19172.

Why is char often treated as integer data type?

Answer»

Character data type accepts and returns all valid ASCII characters. Character data type is often said to be an integer type, since all the characters are represented in memory by their associated ASCII Codes. If a variable is declared as char, C++ allows storing either a character or an integer value.

19173.

…….. relational operators are binary operators.(a) 7(b) 8(c) 6(d) 2

Answer»

6 relational operators are binary operators.

19174.

How many unary operators are available in C++? Give an example.

Answer»

C++ provides two unary operators for which only one variable is required. For Example a = – 50;

19175.

Consider the following code segment. struct student { int rollno; char name[20]; struct date { int day; int month; int year; } birthday; char batch[30]; int score; } Write valid statements in C++ to accept the name and date of birth of a student.

Answer»

gets(S1 .name); 

S1.birthday.day=25; 

S1.birthday.month=5; 

S1.birthday. year=1991;

19176.

Suppose ‘m’ is a oat variable. Another variable ‘ptr’ stores the address of it, which is 2001. After the operation ptr++, it will point to the location having address ………. Why? (a) 2002(b) 2003 (C) 2004 (d) 2005

Answer»

(d) 2005 (add the size of its data type, 2001 + 4)

19177.

Write about Binary operators used in C++.

Answer»

Binary Operators require two operands:

Arithmetic operators that perform simple arithmetic operations like addition, subtraction, multiplication, division (+, -, * , %, /) etc. are binary operators which requires minimum of two operands.

Relational operators are used to determine the relationship between its operands. The relational operators (, >= , <= , == , !=) are applied on two operands, hence they are binary operators. AND, OR (logical operator) both are binary operators. Assignment operator is also a binary operator (+= , – = , *= , /= , %=).

19178.

Identify and correct the error in the following code fragment. struct first { int a; float b; }S1; struct second {int a; float b; }S2; S1 = S2;

Answer»

The assignment S1 = S2 is not right. Because S1 and S2 are different structure variables. C++ does not permit different structure variable assignment.

19179.

Give the syntax to declare an integer 2D array dynamically.

Answer»

int * ptr = new int [m*n]; 

where m and n are rows and columns

19180.

There is no chance of memory leak in static memory allocation”. Say True/False. Justify.

Answer»

The operating system takes the responsibility of memory allocation and deallocation without users instruction.

19181.

Thomas says that name of an array is a pointer by default. Do you agree with him? Justify.

Answer»

Yes. C++ takes the array name as the address of its first element. That is, the name of an array is actually a pointer pointing to the first element of an array.

19182.

Consider the following code segment in C++. If a is stored at memory location 4020 and size of into is 4, then predict the output. int *x, a; x = &amp;a; cout &lt;&lt; x ++ &lt;&lt; “\n”; . cout &lt;&lt; ++ x;

Answer»

The output  is

4020 

4028

19183.

If the name of an array is a pointer to its first element, how to move to third element.

Answer»

Array name +2 gives the address of third element

19184.

Consider the following array initialisation, int ar[5]={-2,-4,-6,2,4}; If first element of array is stored at 2050 and size of int is 4, nd the output of the following.1. cout&lt;&lt; *ar+*(ar+3); 2. cout&lt;&lt; ar+*(ar+4);

Answer»

1. 

*ar = -2

*(ar + 3) = 2 (fourth element) 

Hence *ar + *(ar + 3) = -2 + 2 = 0 

Hence output is 0

2. 

Here ar = 2050 (starting address) 

and * (ar + 4) 

= *(2050 + 4*4) 

= *(2050+16) 

= *(2066) 

= 4 (fth element) 

Hence ar + * (ar + 4) 

= 2050 + 4 

= 2054

19185.

The arrow (→) operator is used for accessing (a) structure element using structure variable. (b) structure element using a structure name. (c) structure element using structure pointer variable.(d) structure element using structure tag.

Answer»

(c) structure element using structure pointer variable.

19186.

Read the following statements. (i) A structure which contains another structure is called self referential structure. (ii) A structure which, contains pointer to another structure is called self referential structure (iii) A structure which contains another structure is called nested structure. (iv) A structure which contains pointer to same structure is called self referential structure Now, choose the correct option from the following (a) statements (i) and (ii) are true (b) statements (i) and (iii) are correct (c) statements (ii) and (iii) are correct (d) statements (iii) and (iv) are correct

Answer»

(d) statements (iii) and (iv) are correct

19187.

Write a program in C++ to copy one string to another without using strcpy() function.

Answer»

#include 

using name space 

std; intmain() 

{ charstr [ ] = “computer”; 

char * sptr; 

sptr = str; 

cout<< str<< endl; 

cout<< sptr<< endl; 

This code copies the string str to sptr;

19188.

Why do we need structure variables, once a structure is defined in program?

Answer»

Structure specification is merely a data type definition. Memory allocation does not take place during specification. To handle data, memory locations are needed. For memory allocation, variables are to be declared. Hence structure variable is essential for handling structured data in programs.

19189.

What are arithmetic operators in C++? Differentiate unary and binary arithmetic operators. Give example for each of them.

Answer»

Arithmetic operators : perform simple arithmetic operations like addition, subtraction, multiplication, division etc.

Unary Operators : Require only one operand .

Example: +, -, * , /, %, >, <, <= , AND,OR

Arithmetic operators : perform simple arithmetic operations like addition, subtraction, multiplication, division etc.

Unary Operators : Require only one operand . 

Example: +, -, * , /, %, >, <, <= , AND, OR

19190.

What will be the result of following statement?char ch= ‘B’;cout&lt;&lt; (int) ch;(a) B (b) b (c) 65 (d) 66

Answer»

Answer is (d) 66

19191.

Calculate the memory requirement for the following structure variable s1. [Consider the size of int = 4, char = 1 and oat = 4] struct student { int mo;char name[25]; float height; }s1;

Answer»
Variable nameNo. of bytes used
mo4 bytes
name25*1 = 25 bytes
height4 bytes
Total33 bytes

The structure variables s1 used 33 bytes memory

19192.

Write a C++ code segment to declare an integer pointer variable named p and store the address of an integer variable  into the pointer variable p.

Answer»

Correct answer is int * p, n; p = &n;

19193.

Consider the following C++ program code to store address of an integer variable a into a pointer variable pint a = 5,*p; P = a; cout&lt;&lt; p; Identify the error in the above program segment and correct the error.

Answer»

P = a is the error because p is an integer pointer and can store only address. The correct code is p = &a;

19194.

Write a C++ statement to declare an array, using character pointer variable, which can store names of 60 students in a class.

Answer»

char*name[60];

19195.

How are the following related? 1. Structure and array2. Structure and class

Answer»

1. Structure and array: 

Structure and array are both derived data types in C++.: a Structure is a user defined data type while an array is a derived data type. Structure is a collection related data items that can be of different data types. Array is also a collection of elements of same data type Thus both array and structured allow several values to be treated together as a single data object. 

2. Structure and class: 

Structure and class are both user defined data types. Structure is a collection of related data items while class is a collection of data and functions. Hence structure plus related functions form a class.

19196.

Which of the following two statements are valid? Why? Also w rite their result, int a; a = 3,014; a= (3,014);

Answer»

It is invalid as comma is not allowed in an integer constant. It is valid. Comma in bracket is allowed.

19197.

Which operator is used to access reference of a variable?(a) $ (b) # (c) &amp; (d) !

Answer»

& is used to access reference of a variable.

19198.

Explain the type of structure defined below and calculate the number of bytes required to store the structure variable s1. [Considerthe size of int as 4 bytes and char as 1 byte] struct student { int mo, mark; char name[25]; struct date { int dd, mm, yy; }doj; }s1;

Answer»

A structure contains another structure, then it is called nested structure.

Variable nameNo. of bytes used
mo4 bytes
mark4 bytes
name25 x 1 = 25 bytes
dd4 bytes
mm4 bytes    doj
yy4 bytes
Total45 bytes

The structure vairable doj used 12 bytes and s1 used a total of 45 bytes memory.

19199.

When a C++ program is executed, the primary memory allocated for it is organized in a particular manner to deal with runtime memory allocation, function calls, variables, etc. Show a diagrammatic representation of it with brief explanation.

Answer»

After compilation of C++ creates four distinct regions of memory used for distinct functions:

STACK 3
HEAP 4
GLOBALProgram
VariablesCode
21

The first area 

(1) is used for storing the compiled code of the program. The second area 

(2) is used for storing global variables of the program. It remains in memory till the program ends. The third region 

(3) known as the stack is used for holding the return addresses of function calls, arguments passed to the function, etc. The last region heap is used for dynamic allocation.

19200.

Assume that in a city, two football matches are going to be held. The coordinators of the match have decided to sell the tickets in two different styles. For the first match, the tickets will be sold one month in advance, but for the second one, tickets will be available from the stadium on the very same day of the match.1. Correlate this situation with the memory allocation techniques in C++. 2. Compare the merits and demerits of the two in relation with the given context.

Answer»

1. Static and dynamic memory allocation. 

2. Every data and instruction data is being executed must be allocated some area in the main memory. The main memory can be allocated in two methods. 

  • Static memory allocation 
  • Dynamic memory allocation

When the amount of memory to be allocated is known in advance and memory is allocated during compilation itself, it is referred to as static memory allocation. When the amount of memory to be allocated is not known in advance and it is required to allocate memory as and when required during run time, it is known as dynamic memory allocation.