cs 240 computer programming 1 variables 1. question 1 : 2 which of the following is a correct c++...
TRANSCRIPT
1
CS 240Computer
Programming 1
Variables
2
Question 1 :
Which of the following is a correct C++ identifier:
SS N1a$total
2D_D s-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
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; ’
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;
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 ; ’?‘ =
7
Question 5:
Write the following algebraic expressions as C+
+ expressions :
𝑏2−4𝑎𝑐
b * 2 – 4 * a * c
8
Question 5:
Write the following algebraic expressions as C+
+ expressions :
𝑤𝑙𝑥2− 22 𝑤𝑥w * l * x * 2 – w * x * 22
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
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;
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}
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;}
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
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
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.
16
Problem 1Answer
17
Problem 1Answer
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.
19
Problem 2Answer
20
Problem 2Answer
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.
22
Problem 3Answer
23
Problem 3Answer