C++ Questions and Answers Part-20

1. What will be the output of the following C++ code?
#include <iostream>
using namespace std;
int func(void *Ptr);
int main()
{
char *Str = "abcdefghij";
func(Str);
return 0;
}
int func(void *Ptr)
{
cout << Ptr;
return 0;
}
a) abcdefghij
b) address of string “abcdefghij”
c) compile time error
d) runtime error

Answer: b
Explanation: Even though it is a void pointer, we gets the address.
Output:
0x8048714

2. What will be the output of the following C++ code?
#include <iostream>
using namespace std;
int main()
{
int *p;
void *vp;
if (vp == p);
cout << "equal";
return 0;
}
a) equal
b) no output
c) compile error
d) runtime error

Answer: a
Explanation: The void pointer is easily converted to any other type of pointer, so these are equal.
Output:
equal

3. What will be the output of the following C++ code?
#include <iostream>
using namespace std;
int main()
{
int i;
char c;
void *data;
i = 2;
c = 'd';
data = & i;
cout << "the data points to the integer value" << data;
data = & c;
cout << "the data now points to the character" << data;
return 0;
}
a) 2d
b) two memory addresses
c) 3d
d) 4d

Answer: b
Explanation: Because the data points to the address value of the variables only, So it is printing the memory address of these two variable.
Output:
the data points to the integer value0xbfc81824 the data now points to the character0xbfc8182f

4. What will be the output of the following C++ code?
#include <iostream>
using namespace std;
int main()
{
int n = 5;
void *p = & n;
int *pi = static_cast< int*>(p);
cout << *pi << endl; return 0; }
a) 5
b) 6
c) compile time error
d) runtime error

Answer: a
Explanation: We just casted this from void to int, so it prints 5
Output:
5

5. What will be the output of the following C++ code?
#include <iostream>
using namespace std;
int main()
{
int a = 5, c;
void *p = & a;
double b = 3.14;
p = & b;
c = a + b;
cout << c << '\n' << p;
return 0;
}
a) 8, memory address
b) 8.14
c) memory address
d) 12

Answer: a
Explanation: In this program, we are just adding the two values and printing it.
Output:
8
0xbfef0378

6. What we can’t do on a void pointer?
a) pointer arithmetic
b) pointer functions
c) pointer objects
d) pointer functions & objects

Answer: a
Explanation: Because the void pointer is used to cast the variables only, So pointer arithmetic can’t be done in a void pointer.

7. Which function is used to check whether a character is an alphabet?
a) isalpha()
b) isalnum()
c) isdigit()
d) isblank()

Answer: a
Explanation: Character classification provides isalpha() function to check whether a character in C++ is an alphabet or not.

8. Which function is used to check whether a character is an alphabet or number?
a) isalpha()
b) isalnum()
c) isdigit()
d) isblank()

Answer: b
Explanation: Character classification provides isalnum() function to check whether a character in C++ is alphabet or number.

9. Which function is used to check whether a character is a number?
a) isalpha()
b) isalnum()
c) isdigit()
d) isblank()

Answer: c
Explanation: Character classification provides isdigit() function to check whether a character in C++ is number or not.

10. Which function is used to check whether a character is a tab or space?
a) isalpha()
b) isalnum()
c) isdigit()
d) isblank()

Answer: d
Explanation: Character classification provides isblank() function to check whether a character in C++ is space or tab.