Daily Quizzes
Mock Tests
Download MCQs Free PDF
Question
Computer Knowledge

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;

}

30

value of x

address of x

Error

नीचे दिए गए C++ प्रोग्राम का आउटपुट है:

#include <stdio.h>

int main (void)

{

int x, *p;

x = 30;

p = x;

print f ("%d", *p);

return 0;

}

30

value of x

address of x

Error

View Answer & Solution
Correct Answer: D (Error)
Detailed Solution:

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.

Thus, the correct answer is: Error

व्याख्या:

आइए दिए गए C++ प्रोग्राम का विश्लेषण करें:

#include <stdio.h>

int main (void)

{

int x, *p;

x = 30;

p = x; // यहाँ एरर है

printf("%d", *p);

return 0;

}

दिए गए कोड में:

• int x, *p; एक इंटीजर x और एक इंटीजर पॉइंटर p को डिक्लेयर करता है।

• x = 30; x को 30 वैल्यू असाइन करता है।

• p = x; पॉइंटर p को इंटीजर वैल्यू x असाइन करने की कोशिश करता है, जो गलत है।

• C++ में, p को p = &x; का इस्तेमाल करके x का एड्रेस असाइन किया जाना चाहिए।

 

लाइन printf("%d", *p); उस वैल्यू को प्रिंट करने की कोशिश करती है जिसे p पॉइंट करता है, लेकिन चूंकि p को सही ढंग से कोई वैलिड एड्रेस असाइन नहीं किया गया है, इसलिए इससे एरर आएगा।

क्योंकि C++ में p = x; इनवैलिड है (आप कास्टिंग या रेफरेंसिंग के बिना सीधे पॉइंटर को इंटीजर असाइन नहीं कर सकते), इसलिए प्रोग्राम सफलतापूर्वक कंपाइल नहीं होगा और कंपाइलेशन एरर देगा।

इस प्रकार, सही उत्तर है: Error

India’s #1 Learning Platform
Start Complete Exam Preparation
Daily Live MasterClasses
Practice Question Bank
Mock Tests & Quizzes
Trusted by 8.1 Crore+ Students

Exam Preparation Simplified

Learn Practice Analyse Improve
Get Started Free
Download the App

Trusted by 8.1 Crore+ Students

Enquire Now

Please share a few details to view the answer and solution.

×
Illustration of two people having a discussion

We're Here for You! Get in Touch with Class24 for All Your Needs!

Disclaimer: Your privacy is important to us. We will not share your information with third parties.