Princess Nora University
College of Computer and Information Sciences
Department of Computer Sciences / CS 241 - Programming 1
Tutorial #1
1st Semester 1432-1433H

Q1

Which of the following is correctly formed constant of type integer?

1 45 / 4. / 12.3 / 34 / ‘8’ / “9” / 12,5

Q2
Declare the following using one statement:

int x;
int y = 7;
int w;
char a = ‘i’;
char b;
char c = ‘E’;

Q3

Given the algebraic equation y= ax3+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;

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

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

Q4.

For the each of the following expressions, give the type and value of its result. Assume the following declarations have been executed before each expression.

int x = 14; int y = 5; int z = 23; int t = 7; double = 5.5 ; double = 8.4;

a.  d/= y %z + x;

b.  x % t < z / y;

c.  --t == t--;

d.  t- = y + z - x/t;

Q5. Trace the following code to find the value assigned to e. Assume that all variables are properly declared.

x = 17;
y = 15;
x = x + y / 4;
z = x % 3 + 4;
w = 17 / 3 + 6.5;
t = x / 4.0 + 15 % 4 - 3.5;

Q6. Trace the following programs and produce the output :

a.  assume the input is 5,2

#include <iostream

using namespace std;

int main()

{

int num1, num2; // declare variables

cout < "Enter two integers: "; // prompt user

cin > num1 > num2; // read values from keyboard

// output the results

cout < "The sum is " < num1 + num2

< "\nThe product is " < num1 * num2

< "\nThe difference is " < num1 - num2

< "\nThe mod is " < num1 % num2 < endl;

return 0; // indicate successful termination

}

b.

#include <iostream>

using namespace std;

int main ()

{

// Part A

cout < "1 2 3 4\n";

// Part B

cout < "1 " < "2 " < "3 " < "4\n";

// Part C

cout < "1 ";

cout < "2 ";

cout < "3 ";

cout < "4" < endl;

return 0;

}

Q7
Write the following algebraic expressions as C++ expressions:

b2-4ac

yx2-y2x2+3xy

Q8

Correct the errors in the following programs:

#include<iostream>;
using namespace std;
int main();
{
int x; y=2;
x=y+z;
cout<”x”; //print the integer x
}
#include<iostream
using name space std;
int main
{
char a=”$”;
cout<a;
cin>b;
cout<b,
return o;
#include<iostream
using namespace sta;
int main()
{
char x;
int y;
int z, w;
y = x+z;
x=’f’;
cout<’x’; // print x value
return 0;
}
#include<iostream
using namespace std;
int main()
{
int s1=5, s2=3;
float s3 = 8.5;
s1 = s2 + s3;
s3 = s1 + s2;
return 0;
}
#include<iostream
using namespace std;
int main()
{
int s1=5, s2=3;
float s3 = 8.5;
s1 = s2 + s3;
s3 = s1 + s2;
return 0;
}
#include<iostream
using namespace std;
int main()
{
float total, a1=2.5, a2=3.84;
Total = a1+a2;
int numOfStudents = 100;
coutnumofStudents;
return 0;
}