Question

Question 1 (8 points): Choose the Correct Answer (2 points for each question) 1. What are the values of the variables x and y
0 0
Add a comment Improve this question Transcribed image text
Answer #1

1.

int x=0, y=2;
if(x==0)
{   y=1;}
else
{   y=2;}


since initial value before if condition evaluation of x is 0, the if condition is satisfied (true)
and thus the code inside if block executes .. therefore y is assigned a value 1.
since if condition was true, else block won't be executed
therefore, the values of x and y afterwards will be (a) x=0, y=1;

-----------------------------------------------------

2. (a) this will output "Yes" because both the conditions are satisfied and with the OR operator, even if one is satisfied, it will be true

(b) this will output "Yes" only for num=5, the if condition will be false for as greater than equal to 5 is false when num is 2 or 3 or 4

(c) this will output "Yes" as both the conditions will always be true for num=2 or 3 or 4 or 5

(d) compilation error, number can't be compared this way together with two conditions

-----------------------------------------------------

3. 0


The second if clause is nested inside the first if clause
and the else condition will correspond to the second nested if clause only

First if condition is not satisfied as x=9 so x can't be less than 5
therefore none of the statements further are executed and the value of y remains 0.

-----------------------------------------------------

4. (b) 0 2

x-- will decrement the value of x by 1 after using the previous value of x for calculation (as this is port decrement operator)
thus, x's value is 1 for calculation y= 1* 2
so, y =2
and then x will be decremented to 0.

Add a comment
Know the answer?
Add Answer to:
Question 1 (8 points): Choose the Correct Answer (2 points for each question) 1. What are...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • What is the output of the following code snippet? (If there is some kind of syntax...

    What is the output of the following code snippet? (If there is some kind of syntax error indicate this by marking "error" in your answer choice) 1.        int fly = 5; int x;        if (fly-- > 5)               x = 5;        else               x = 2;        if (x++ > 3)               cout << x;        cout << fly << endl; 2.   int i = 0;        bool b = i == 0 || i++ > 0;        if (!b)               cout << i << endl;        else               cout...

  • Example (4) Trace the following program and find the output >> SOURCE CODE #include <iostream.h> int...

    Example (4) Trace the following program and find the output >> SOURCE CODE #include <iostream.h> int main0 // define two integers int x-3; int y = 4; //print out a message telling which is bigger if (x >y) i cout << "x is bigger than y" << endl: else cout << "x is smaller than y" << endl; return 0; Example (5) Write a C++ program that takes from the user a number in SR (Saudi Riyal) then the program...

  • 18 C++ 1. What is the output of the following program segment? int y-22: while ((y...

    18 C++ 1. What is the output of the following program segment? int y-22: while ((y 3) != 0) cout << y<< "": y=y-2; The output is 2. Suppose that the input is 100, 20,-8, 50, 20. What is the output of the following C++ code? int sum0 int num: int j cin >> num: if (num < 0) continue зит зит + num; cout<< sum << endl; The output is

  • A. What is the output of the following C++ code fragment? (all variables are of type...

    A. What is the output of the following C++ code fragment? (all variables are of type int) int count-1; int y-100; while (count 3) y=y-1 ; count+t cout << y << endl; cout<< count <<endl What is the value of x after control leaves the following while loop? (all variables are of type int) B. int x0 while (x < 20) sum- sum+x cout << sum<< endl;

  • (C++) Given the following code, please help me answer these three parts and please explain as...

    (C++) Given the following code, please help me answer these three parts and please explain as simply as you can. 1.) How many iterations of the innermost loop? (please show calculation) 2.) What is the largest output? 3.) How many zeroes at the start? (please show/explain calculation) for (int y 0; y 〈 4; y++) for (int z 0: z 〈 5; z++) cout << x * y * z くく endl;

  • plz solve 2 ,4,8,10 " << endl; acement display? Ans: answer= B. answer 2. cu int...

    plz solve 2 ,4,8,10 " << endl; acement display? Ans: answer= B. answer 2. cu int x = 2, y = 1; what will the following cout statement display? Ans: cout << "answer=" < (x || !y ) << endl; A. answer=0 B. answer=1 C. answer=2 D. None of these This code void ActivateBuzzer(int); is c A. to call a function B. function definition (C.)function declaration D. not valid Which option correctly describes the result of the following C/C++ code?...

  • Beginner Level Programming Question Consider the following scenario Number of Shirts Discount 5-10 10% 11-20 15%...

    Beginner Level Programming Question Consider the following scenario Number of Shirts Discount 5-10 10% 11-20 15% 21-30 20% 31 or more 25% The following C++ code segment that prompts the user for the number of shirts required and then display the discount. Assume that the discount is zero if number of shirts is less than 5. For example, if the your enter 15, then your code should output 15% Please select the correct statements A, B, C int shirts cin...

  • It's a C++ code. 2. Answer the following questions in a word document. 6. What is...

    It's a C++ code. 2. Answer the following questions in a word document. 6. What is the output of the following C++ code? (2, 3) int inti - 26 int int2 = 45; int intPtr - Einti; int int2Ptr - Gint2: intPtr -89; int2Ptr - 623 intiptr - int2Ptr intPtr - 80 intl -57 cout << inti «..« int2 << endl; cout << *intiptr «« int2Ptr << endl; 7. Given the following statements: int num; int numPtr write C++ statements...

  • 12) 8 pts. Find the errors in the following code fragment and correct them. #i nclude...

    12) 8 pts. Find the errors in the following code fragment and correct them. #i nclude <iostream> using namespace std; double fudge (double s) f return s 2.3; int mainO cout >> "Your original estimate" double estimate; cin >> estimate; cout << endl; for (int 1 = 0;1c3;i++); cout << "EStimate' < fudge(estimate) <<endl Hint: There are 4 syntax errors. There is one error that is syntactically correct but causes the output to not be what you would expect. Once...

  • 5. (6 pts) What is the outputs of following section of a program? int a[10] =...

    5. (6 pts) What is the outputs of following section of a program? int a[10] = {1,2,3,2, 5, 6, 7, 8 , 9, y): int i - 0, 30: while (i<10) coutくく3くくendl ; 6. (6 pts) What is output of following set of instructions in a C++ program: (In this problem both x and y represent positive integers) int a[10]={-1, 3=0; 2, 7, -4, 11, -2, 3, -9); x, -y, int for (int i-0:i<10:i++) if (a [1]<0) s+ai] coutくく3(くend-; 7....

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT