Q47.Marks: +2.0UGC NET Paper 2: Computer Science 18th June 2024 Shift 1 (Cancelled)
The output of the following C++ Program is:
#include <stdio.h>
int main (void)
{
int x, *p;
x = 30;
p = x;
print f ("%d", *p);
return 0;
}
1.30
2.value of x
3.address of x
4.Error✓ Correct
Solution
The correct answer is Error
Explanation:
Let's analyze the provided C++ program:
#include <stdio.h>
int main (void)
{
int x, *p;
x = 30; p = x; // Error here
printf("%d", *p);
return 0;
}
In the given code:
int x, *p; declares an integer x and a pointer to an integer p.
x = 30; assigns the value 30 to x.
p = x; attempts to assign the integer value x to the pointer p, which is incorrect.
In C++, p should be assigned the address of x using p = &x;.
The line printf("%d", *p); attempts to print the value pointed to by p, but since p is not correctly assigned a valid address, this will result in an error.
Because p = x; is invalid in C++ (you cannot assign an integer directly to a pointer without casting or referencing), the program will not compile successfully and will result in a compilation error.