Answer and explain why please! 1) What is the result of the example below, where
ID: 3858198 • Letter: A
Question
Answer and explain why please!
1) What is the result of the example below, where a semicolon immediately follows the condition of the if statement?
if (14 % 2 == 1);
cout << "14 is odd" << endl;
a) no output b) it prints "14 is odd" c) it prints "14 is even" d) an error message
2) What would be the result of the following code?
string str = "apothecary";
cout << str.at(str.length()) << endl;
a) it would print "a" b) it would print "r" c) it would print "y" d) an error message
3) Assume that we initialize num prior to this section of code. Which value of num will cause it to print out "true"?
a) 6 b) 12 c) 17 d) 63
4) The following code is meant to write some text to a file, but right now it won't. Which line needs to be changed for that to happen?
a) line 1 b) line 2 c) line 3 d) line 4
Explanation / Answer
1) B
//if 14 is divisible by 2 and gives 1 as remainder
//then the inner statement will execute
if(14%2==1)
cout<<"14 is odd"<<endl;
//But here
//semicolon is placed at the end of the if condition
//that means,the if statement is terminated
//the inner statement is not inside the if condition anymore
//it is actually a independent statement
//so,the if condition fails or not,
//the cout statement will definatly executes
//therefor it will prints the "14 is odd"
if(14%2==1);
cout<<"14 is odd"<<end1;
2) D
//str is string, which contains the word "apothecary"
//which is length 10, and index 0 to 9
//the function str.at() will take the index as parameter and
//checks whether pos is the valid position of a character or not
//for example
cout<<str.at(4)<<endl;
//will gives the output as "h;
//but here we passing str.length() as index
//str.length() will gives you output as 10
cout<<str.at(str.length())<<endl;
//which is equal to
cout<<str.at(10)<<endl;
//index 10 is not there in the string str
//there for it will prints an error message
3) C
//The program shoud print the output as true
//in that case,it should satisfy the if condition after the for loop
if(result)
cout<<true<<endl;
//if it needs to satisfy, result should be true
//in initial, result is intialized with true only
bool result=true;
//therefore it will print true as output
//if it is not entered into for loop or even entered
//it should not satisfy the if condition
for(int i=2;i<num;i++)
if(num%i==0)
result=false;
//for loop is iterating from i=2 to the entered number num
//and if it is divisible by any of the number between 2 to num
//if condition will satisfy and result will false and output will be as false
if we observed this,the num should not be divisible by any number between 2 to num
that is nothing but prime number
so num should be prime number
from the given option prime number is only 17
int num=17
4) A
ifstream is used to read from file
ofstream is used to write on file
line 1 should be changed, to write on file
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.