Review Questions CSE 100
1. Write the code snippet for taking a user input (string) and storing each character into an array
called letters.
string input;
cout > input;
int length = input.length();
cout
Review Questions CSE 100
1. Write the code snippet for taking a user input (string) and storing each character into an array
called letters.
string input;
cout << "enter the string";
cin >> input;
int length = input.length();
cout << "Length: " << length << endl;
int SIZE = length;
char a [100];
for (int i =0; i < length; i++)
a[i] = input.at(i);
for (int i=0; i < length; i++)
cout << a[i] << " " ;
2. What is cout< (number);
4. Which formatting function helps control the width of the output.
setw()
5. Write a function called bool isPrime(int number) which finds out whether a given number is
prime or not and returns true accordingly.
bool isPrime(int number)
{
bool flag = false;
for (int i = 2; i
using namespace std;
int h( int, int & );
int main()
{
int c = 1, d = 3;
cout << h( c, d ) << endl; // Line 1
cout << h( d, c ) << endl; // Line 2
cout << c << endl; // Line 3
}
int h( int a, int & b )
{
b = b - 1;
if (b > 0)
cout << b << endl; // Line 4
if (a > 1)
a--;
return a;
}
7. What value is displayed the first time Line 4 is executed?
2
8. What value is displayed when the cout stream in Line 2 is executed?
1
9. What value is displayed when Line 3 is executed?
0
10. Given the following:
int a[5][7],b[5][7];
Which of the following statements are correct:
a =b ; //Incorrect
a[1][2]=b[3][4]; // Correct
a[0][-1] = -1; // Incorrect
11. Write the specification file for the UML class given below:
Student
-Name :string
-Id:string
-department:string
+Student(name,id)
+getName():string
+getId():string
+setDept(string):void
class Student {
private:
string Name;
string Id;
string department;
public:
Student(string,string);
string getName();
string getId();
void setDept(string);
}
12. Instantiate an object of the class above and call the setDept() method with the necessary arguments.
Student student1(“Smith”, “123456”);
student1.setDept(“CSE”);