You are on page 1of 3

ASSINGMENT QUESTION ANSWER

1. Why C++ Programming is better than C programming language?


Explain with an example with your own logic.

Solution:
"C ++ is an advanced object-oriented programming language known for its
adherence to structured principles and fixed syntactical constructs. It offers
a balance between code simplicity and expressiveness. Unlike its
predecessor, the C programming language, C++ embraces the concept of
encapsulation, promoting the protection of data and methods from
external interference. This design contributes to enhanced code security.
Overall, C++ is recognized for its robustness in enabling efficient, secure,
and well-organized programming practices."

For Example
The main difference between C and C++ is that C++ was developed as an
extension of C. C++ was designed to be easier to use and to allow programmers
to make efficient use of computer resources. C++ also has some similarities with
C, but there are some important differences. C++ is a good choice for
experienced programmers who want to learn a new programming language.
Well, we will be first looking at the core of what is C and C++ followed by a
tabular representation of detailed head-to-head comparison.
ASSINGMENT QUESTION ANSWER

2. Why the following code every time gives different output when you run it

multiple times? Explain with your own logic.

#include <iostream>

using namespace std;

int main() {

int score = 5;

cout <<&score <<endl;

return 0;

Solution:
The code prints the memory address of the ‘ score ‘variable. Modern
systems use address randomization for security, so the address changes
each time you run the program. This is to prevent attackers from predicting
memory locations. As a result, the output varies each time you run the
code.
ASSINGMENT QUESTION ANSWER

3. There are 3 jars, namely, A, B, C. All of them are mislabeled. Following


are the labels of each of the jars:A: CandiesB: SweetsC: Candies and
Sweets (mixed in a random proportion) You can put your hand in a jar and
pick only one eatable at a time. Tell the minimum number of eatable(s) that
has/have to be picked in order to label the jars correctly. Explain the answer
with your own logic.

Solution:

To correctly label the mislabeled jars A, B, and C, you can take the following steps:

Step 1: Choose a random jar, say jar A (labeled "Candies").

 Since all jars are mislabeled, there are two possibilities for the contents of jar A: it could
either contain candies, or it could contain a mix of candies and sweets.
 If you pick a candy from jar A, then it must be full of candies (because it cannot be the
mixed jar). Thus, it should be labeled "Candies."
 If you pick a sweet from jar A, then it cannot be labeled "Candies" because the jar that is
labeled "Candies" must only contain candies. This means jar A must actually be the
mixed jar, so it should be labeled "Candies and Sweets."

Step 2: Now that you've correctly labeled jar A, you can deduce the labels for jars B and
C.

 Jar B, which is labeled "Sweets," cannot be the mixed jar, as the mixed jar has already
been identified as jar A. So, jar B must contain only sweets.
 Jar C, which is labeled "Candies and Sweets," cannot actually be the mixed jar since
you've identified jar A as the mixed jar. Therefore, jar C must actually contain only
sweets.

In conclusion, by picking just one eatable from jar A and making logical deductions, you
can correctly label all three jars.

You might also like