اسئلة واجوبة لسنوات سابقة
  أستــاذ المــادة  هديل قاسم غني
  تـاريـخ النشــر   10/04/2017
Share |


اسئلة امتحان المودل الاول الخاص بعبارتي IF و SWITCH :-

Q1// Consider the following two pieces of codes and choose the best answer:

 

CODE 1:

switch (x) {

case  1:

cout <<”x is 1”;

break;

case 2:

                cout <<”x is 2”;

                break;

default:

                cout <<”value of x unknown”;

}

 

CODE 2

If (x==1){

                Cout <<”x is 1”;

                }

Else if (x==2){

                Cout << “x is 2”;

                }

Else{

                Cout <<”value of x unknown”;

}             

 

a.       Both of the above code fragments have the same behaviour

b.      Both of the above code fragments produce different effects

c.       The first code produces more results than second

d.      The second code produces more results than first.

Answer: a .

 

Q2// Observe the following block of code and determine what happens when x=2?

switch (x){

case 1:

case 2:

case 3:

                cout<< "x is 3, so jumping to third branch";

default:

                cout<<"x is not within the range, so need to say Thank You!";

                }

a. Program jumps to the end of switch statement since there is nothing to do for x=2

b.      The code inside default will run since there is no task for x=2, so, default task is run

c.       Will display x is 3, so jumping to third branch.

d.      None of above

 

Answer: c

 

Q3// Find out the error in following block of code:

 

If (x = 100)

    Cout << “x is 100”;

a.       100 should be enclosed in quotations

b.      There is no semicolon at the end of first line

c.       Equals to operator mistake

d.      Variable x should not be inside quotation

 

Answer: c

 

Q4// Suppose that the value of a is equal to seven and b is equal to twelve, what is the output of the following C++ Program?

 

#include <iostream>

#include <conio.h>

using namespace std;

void main( )

{

    int a, b;

    char x= + ;

    cout << "Enter two numbers"<<"\n";

    cin >> a >> b;

    switch ( x )

    {

         case  + : cout << a + b;

         break;

         case  - : cout << a - b;

         break;

         case  * : cout << a * b;

         break;

         case  / : cout << a / b;

         break;

         default: break;

    }

    cout<<"\n";

}

 

a) 19    b) 22    c) 5  d) Compilation error

 

Answer: a