Better programmer is he who has better programming skills, for programming skills one needs to PROGRAM because only reading won’t get you anywhere. From regular programming comes good programming skill.
Solving problems basically means to write a program which performs the actions as per the question. But the problems listed here are different; these problems will check your understanding of the C++ programming language. You don’t need to write any program.
In this article I have listed some of the good problems that I have come across, which are related to what we discussed so far (variables, data types etc.).
This article is basically for beginners but even if you are an experience C++ programmer, you might want to look at them.
Problem No. 1
//C++ Program
#include<iostream.h>
void main(void)
{
int arr[5]={2,3};
cout<<arr[2]<<arr[3]<<arr[4];
}
QUESTION: What will be the output of this program?
Problem No.2
//C++ Program
#include<iostream.h>
void main(void)
{
int x=10,y=20,z=5,i;
i=x<y<z;
cout<<i;
}
QUESTION: What will be the output of this program?
Problem No.3
//C++ Program
#include<iostream.h>
int i=10;
void main(void)
{
int i=20;
cout<<i;
}
QUESTION: What will be the output of this program?
Problem No.4
//C++ Program
#include<iostream.h>
void main(void)
{
int i=10;
{
int i=20;
cout<<i;
}
}
QUESTION: What will be the output of this program?
Problem No.5
//C++ Program
#include<iostream.h>
void main(void)
{
union un
{
short int i;
char ch[2];
};
union un u;
u.ch[0]=3;
u.ch[1]=2;
cout<<u.i;
}
QUESTION: What will be the output of this program?
Answers:
1. 000, when an array is partially initialized, the remaining elements are automatically filled with 0s.
2. 1, look at the statement i=x<y<z;, here first x<y is evaluated whose results is TRUE (or 1) then 1<z is evaluated which is again TRUE(or 1), therefore the value 1 is put into the variable x.
3. 20, since the variable which is local is given the priority.
4. 20, the variable which is more local is given the priority.
5. 515, HINT: short int is two bytes long and the character array (two bytes as a whole) shares it.
Related Articles: