Top 8 interview questions about variables| conceptual questions

c++ interview questions


This article covers all the tricky and conceptual questions related to the c++ variables that are likely to be asked in interviews. We also have a detailed article with programs about c++ variable. The main purpose is to elevate your concepts and strengthen your basics. If you are learning to program then you should know the answers to these questions.

What is the difference between the declaration and initialization of a variable?

When we talk about the variable declaration, we basically reserve space in memory depending upon the variable type. Every type has a different size and needs different space in memory. For example, int requires 4 bytes.
Initialization means storing some initial value in the variable that can be used in the program. Values stored in the variables are actually stored in the reserved space in memory for that variable.


#include <iostream>
using namespace std;
int main()
{
 char a, b, c;
 int num1, num2;  // declaration
 float f1, f2;

 //--------------------------------
 a = b ='n';
 c = 't';

 num1 = num2 = 100; //initialization

 f1 = f2 = 25.5;

 system("pause");
 return 0;
}


What is the difference between local and global variables?

The variables declared inside a block are called local variables for the block. Local variables can be accessed only within the braces in which it is declared. These variables do not exist outside the curly braces.
The variables declared outside all the blocks of code or outside all the functions are global variables. These variables can be accessed in all the blocks or functions. In other words, they can be assessed throughout the file.


#include <iostream>
using namespace std;

int marks1=10;//global variable
int main()
{
 int marks2 = 15;//local
 if (true) {
  int marks3 = 20;//local
 }
 system("pause");
 return 0;
}
In the above code, marks1 can be accessed throughout the source file, marks2 can be accessed throughout the main, and marks3 can only be accessed inside the block of if statement.

If there are a global variable and a local variable with the same name, which one has higher precedence?

In order to understand this concept, let's consider the below code.

#include <iostream>
using namespace std;

char capital='A';//global 
int main()
{
 char capital = 'B';//local
 cout << capital;
 system("pause");
 return 0;
}


As you can see the above code has a local and a global variable with the same name capital. In this situation, the compiler prefers the local variable on the global variable. That's why the output of the program is B rather than A.

How to access a local or a global variable if they have the same name in a program?

If we have a local and a global variable in a program with the same name then we can access the global variable with a scope resolution operator. By default, precedence is given to the local variable with the name. By using the scope resolution operator, the compiler lets you access the global variable.

#include <iostream>
using namespace std;

int marks=10;
int main()
{
 int marks = 15;
 if (true) {
  int marks = 20;

  cout << marks;//accessing local#1
 }
 cout << marks;//accessing local#2 
 cout << ::marks;//accessing global

 system("pause");
 return 0;
}
Let's take an example of the above code. we have three variables with the same name marks. One is local with respect to if statement block, the second one is local with respect to the main function block and the third one is the global variable. We wrote a cout statement within if block to access local variable#1. Then we wrote another cout statement outside if block to access local variable#2.
Then we used the scope resolution operator to access the global variable.

How to declare and define a constant in c++?

It is important to first understand what are constants in c++. The value of a constant remains unchanged throughout the program that's why its called constant. For example the value of pi.

In c++ constant is declared and initialized using the const keyword. It is the same as variables but its value cannot be changed throughout the program execution. It can only be used in operations with other variables like multiplication.


#include <iostream>
using namespace std;
int main()
{
 const float pi = 3.14;//constant
 float r, AreaofCircle;
 AreaofCircle = pi * r * r;
 cout << "Area of the circle:\t" << AreaofCircle;

 system("pause");
 return 0;
}

Is there any difference between the lifetime and scope of a variable?

The lifetime of the variable is the time when the variable is declared to its time of destruction. On the other hand scope of the variable is the point where the variable is accessible. So the lifetime and scope are two different things and it is also possible for a variable to have different lifetime and scope which is explained in the next question.

Is it possible for a variable to have different scope and lifetime?

Yes, a variable can have different scope and lifetime. Take an example of a variable declared with a static keyword. As we know that the lifetime of a variable declared with the static keyword is from the start till to the end of the program. But if this static variable is declared inside the braces of the function then it is accessible only inside the block which means that it has local scope. It means that this variable exists from the start till to the end of the program but it can only be accessed inside of the block. In other words, it has local scope and a global lifetime.

What is the difference between external and global variables?

Global variables can be globally accessed by any block of code in one source file. But it can not be accessed in other source files of the same module or project. In order to share the global variable with other source files, we use the extern keyword to access the global variable of other source files.

That's it for this article. If you have any conceptual questions about variables then write it in the comments below. I will surely answer your question. Thanks for reading and supporting us.

Post a Comment

0 Comments