CS 240 Computer Programming 1 Variables 1. Question 1 : 2 Which of the following is a correct C++...

23
CS 240 Computer Programming 1 Variables 1

Transcript of CS 240 Computer Programming 1 Variables 1. Question 1 : 2 Which of the following is a correct C++...

Page 1: CS 240 Computer Programming 1 Variables 1. Question 1 : 2 Which of the following is a correct C++ identifier: SSN1a$total2D_Ds-3_ds.

1

CS 240Computer

Programming 1

Variables

Page 2: CS 240 Computer Programming 1 Variables 1. Question 1 : 2 Which of the following is a correct C++ identifier: SSN1a$total2D_Ds-3_ds.

2

Question 1 :

Which of the following is a correct C++ identifier:

SS N1a$total

2D_D s-3 _ds

Page 3: CS 240 Computer Programming 1 Variables 1. Question 1 : 2 Which of the following is a correct C++ identifier: SSN1a$total2D_Ds-3_ds.

3

Question 2 :

Which of the following is correctly formed constant of

type integer?

145 4.0 12.3 34 ‘8’ “9” 12,5

Page 4: CS 240 Computer Programming 1 Variables 1. Question 1 : 2 Which of the following is a correct C++ identifier: SSN1a$total2D_Ds-3_ds.

4

Question 3 :

Declare the following using one statement:

int x;int y;int z;

 

char a = ‘i’;char b;char c = ‘E;’

 

int x , y , z;

char a = ‘i’ , b , c = ‘E; ’

Page 5: CS 240 Computer Programming 1 Variables 1. Question 1 : 2 Which of the following is a correct C++ identifier: SSN1a$total2D_Ds-3_ds.

5

Question 4 :

Write a C++ statement to do the following :

a) Declare two variables of type character, each one is in a separate declaration statement .

char a;

char b;

Page 6: CS 240 Computer Programming 1 Variables 1. Question 1 : 2 Which of the following is a correct C++ identifier: SSN1a$total2D_Ds-3_ds.

6

Question 4 :

Write a C++ statement to do the following :

b) Declare two variables of type character and initialize them with the values $and ? respectively in the declaration. NOTE: use one declaration statement .

char c = ‘$’ , d ; ’?‘ =

Page 7: CS 240 Computer Programming 1 Variables 1. Question 1 : 2 Which of the following is a correct C++ identifier: SSN1a$total2D_Ds-3_ds.

7

Question 5:

Write the following algebraic expressions as C+

+ expressions :

𝑏2−4𝑎𝑐

b * 2 – 4 * a * c

Page 8: CS 240 Computer Programming 1 Variables 1. Question 1 : 2 Which of the following is a correct C++ identifier: SSN1a$total2D_Ds-3_ds.

8

Question 5:

Write the following algebraic expressions as C+

+ expressions :

𝑤𝑙𝑥2− 22 𝑤𝑥w * l * x * 2 – w * x * 22

Page 9: CS 240 Computer Programming 1 Variables 1. Question 1 : 2 Which of the following is a correct C++ identifier: SSN1a$total2D_Ds-3_ds.

9

Question 6: Each of the following

assignment statement contains at least one error. Identify

them : D e l t a = x ( y + ( 3 x +

( z + 1 5 ) ) )

X = 5 = y + 4

Page 10: CS 240 Computer Programming 1 Variables 1. Question 1 : 2 Which of the following is a correct C++ identifier: SSN1a$total2D_Ds-3_ds.

10

Question 7:Given the algebraic equation y=a +7 , which of the following, if andy are correct C++ statements

for this equation?

a. Y= a*x*x*x+7;

b. Y=a*x*x*(x+7);

c. Y=(a*x) * x*(x+7);

d. Y=(a*x)*x*x+7;

Page 11: CS 240 Computer Programming 1 Variables 1. Question 1 : 2 Which of the following is a correct C++ identifier: SSN1a$total2D_Ds-3_ds.

11

Question 8:Correct the errors in the

following programs:

#i n c lude< ios t ream>;us ing namespace s td ;

in t ma in ( ) ;{ i n t x ; y=2;

z= 4 ; x=y+z ;

cout<<”x ; ”/ /pr in t the in teger x

}

#i n c lude< ios t ream>us ing namespace s td ;

in t ma in ( ){ i n t x , y=2 , z=4 ; x=y+z ;

cout<<x ;/ / pr in t the in teger

x}

Page 12: CS 240 Computer Programming 1 Variables 1. Question 1 : 2 Which of the following is a correct C++ identifier: SSN1a$total2D_Ds-3_ds.

12

Question 8:Correct the errors in the

following programs:

#include<iostream>using name space std;

int main{

char a=”$”; cout<<a; cin>>b; cout<<b, return o;

#include<iostream>using name space std;

int main ( ){ char a=‘$’ , b; cout<<a; cin>>b; cout<<b; return 0;}

Page 13: CS 240 Computer Programming 1 Variables 1. Question 1 : 2 Which of the following is a correct C++ identifier: SSN1a$total2D_Ds-3_ds.

13

Question 9:What is the output by each of the following code fragment?

int x=1;

int main(){

int x=3;cout<<x<<endl;cout<<::x<<endl;return 0;

}

The output :

3

1

Page 14: CS 240 Computer Programming 1 Variables 1. Question 1 : 2 Which of the following is a correct C++ identifier: SSN1a$total2D_Ds-3_ds.

14

Question 9:What is the output by each of the following code fragment?

int a=1;

int b=-3;

int c=2;

b=a++;

c+=--a+b++;

cout<<a<<" "<<b<<" " <<c;

The output :

1 2 4

Page 15: CS 240 Computer Programming 1 Variables 1. Question 1 : 2 Which of the following is a correct C++ identifier: SSN1a$total2D_Ds-3_ds.

15

Problem 1

Question

Write a program that asks the user to input 2 numbers A and B, then exchange the values of A and B.

Page 16: CS 240 Computer Programming 1 Variables 1. Question 1 : 2 Which of the following is a correct C++ identifier: SSN1a$total2D_Ds-3_ds.

16

Problem 1Answer

Page 17: CS 240 Computer Programming 1 Variables 1. Question 1 : 2 Which of the following is a correct C++ identifier: SSN1a$total2D_Ds-3_ds.

17

Problem 1Answer

Page 18: CS 240 Computer Programming 1 Variables 1. Question 1 : 2 Which of the following is a correct C++ identifier: SSN1a$total2D_Ds-3_ds.

18

Problem 2Question

Write a program that reads in the length and the width of a

rectangular yard. Your program should compute the time required (in minutes) to cut

the grass at the rate of 2.3 square meters per a second.

Page 19: CS 240 Computer Programming 1 Variables 1. Question 1 : 2 Which of the following is a correct C++ identifier: SSN1a$total2D_Ds-3_ds.

19

Problem 2Answer

Page 20: CS 240 Computer Programming 1 Variables 1. Question 1 : 2 Which of the following is a correct C++ identifier: SSN1a$total2D_Ds-3_ds.

20

Problem 2Answer

Page 21: CS 240 Computer Programming 1 Variables 1. Question 1 : 2 Which of the following is a correct C++ identifier: SSN1a$total2D_Ds-3_ds.

21

Problem 3Question

Write a program that asks the user to type the price without tax of one kilogram of tomatoes, the number of kilograms you want to buy and the tax in percent units. The program must calculate the

total price including taxes.

Page 22: CS 240 Computer Programming 1 Variables 1. Question 1 : 2 Which of the following is a correct C++ identifier: SSN1a$total2D_Ds-3_ds.

22

Problem 3Answer

Page 23: CS 240 Computer Programming 1 Variables 1. Question 1 : 2 Which of the following is a correct C++ identifier: SSN1a$total2D_Ds-3_ds.

23

Problem 3Answer