Solutions of C++ Quiz 1

Want to solve C++ Quiz 1, Click here
In this post, we will see the Answers and explanation of questions in C++ Quiz 1.


Q.1. C++ is an alias of C#?
True
False
C++ is not the alias of C# programming language.

Q.2. What data type is used to create a variable that should store text?
string
String
txt
char

string (small s) data type is used to create a variable that should store text.

#include <iostream>
#include <string>

using namespace std;

int main() {
    string str = "CsCode";
    cout<<str;

    return 0;
}

Q.3. How do you create a variable with the floating number 2.8?
x = 1.3;
int x = 1.3;
double x = 1.3;
byte x = 1.3;


x = 1.3; is not valid because it was not declared in the scope. int stores only integer values.
We use double data type to store floating point numbers.

Q.4. The value of a string variable can be surrounded by single quotes.
True
False

Value of char is surrounded by single quotes.

#include <iostream>
#include <string>

using namespace std;

int main() {
    string str = "CsCode";
    char ch = 'a';
    
    cout<<str<<endl<<ch;

    return 0;
}

Q.5. Which statement is used to stop a loop?
exit
continue
break
stop

break statement is used to stop a loop. continue statement continues with the next iteration in the loop.

#include <iostream>
using namespace std;
int main() {
    int m = 5; 
    while(1){
        if(m==1){
            break;
        }
        m--;
    }
    cout<<m;
    
    return 0;
}

Q.6. Choose the correct output

#include <iostream>

using namespace std;

int main() 
{

    int m=11;
    m += 1.1;
    cout<<m;
    return 0;
}

11
12.1
12.11111
12

The output of above C++ code is 12. Initially the value of m is 11 and m is of type int. We added 1.1 to m so it becomes 12.1 but m is of type integer, 12.1 is explicitly type cast into integer as 12 .

Q.7. Find the correct output below C++ Code

#include <iostream>

using namespace std;
void a(int s);

int main()
{

    int s=48;
    a(s);   //s is actual parameter
    cout<<s;
    return 0;
}

void a(int s)   // This s is formal parameter of function a()
{
    s = s+10;
}

58
48
0
40

Pass by value concept is used in the above code. In pass by value, changes in formal parameters does not affects actual parameters.

Q.8. Choose the correct output

#include "iostream"
#include "cstring"
using namespace std;

int main() 
{
    char a[] = "Com\0puter";
    cout<<strlen(a);
    return 0;
}

8
9
3
4

\0 acts as a string terminator. If you declare a string s = “CsCode”; then \0 is automatically added to the end of string s.
So the output of above code is 3.
 

Q.9. Guess the output of code below

#include "iostream"
using namespace std;

int main() 
{
    int m=1;
    
    if(m == 2);
    cout<<"Hii";
    
    cout<<" Hello";
    return 0;
}

Hello
Hii
Hii Hello
Hello Hii

There is semicolon ; after if statement. Semicolon means end of the statement so the scope if block is only if(m==2);.
Then Hii is printed after that Hello is printed.

Q.10. Objects created using new operator are stored in __ memory.
Stack
Cache
Heap
None

Stack : All variables declared inside the function will take up memory from the stack.
Heap : The memory is allocated during the execution of program.


If you have any doubts, ask us on Instagram or comment below.


CsCode

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top