Question

In: Computer Science

(True or False) The following function will compile. void print(int num) { return num+1; } Group...

(True or False) The following function will compile.

void print(int num)
{
    return num+1;
}

Group of answer choices

True

False

Flag this Question

Question 2 10 pts

(True or False) The following code will output "I was true".

bool isGreater(string s1, string s2)
{
    if(s1 > s2)
    {
        return true;
    }

    return false;
}

int main()
{
   if(isGreater("before","back"))
   {
      cout << "I was true";
   } else {
      cout << "I was false";
   }

   return 0;
}

Group of answer choices

True

False

Flag this Question

Question 3 10 pts

What is the ouput of the following code?

void loop(int num)
{
   for(int i = 1; i < num; ++i)
   {
       for(int j = 0; j < 5; ++j)
       {
           cout << j;
       }
   }
}

int main()
{
   loop(3);
   return 0;
}

Flag this Question

Question 4 10 pts

What is the output of the following code?

void myFunc(int n)
{
  n = 10;
}

int main()
{
   int num = 4;
   myFunc(num);
   cout << num;
}

Flag this Question

Question 5 10 pts

What is the output of the following code?

void myFunc(int &n)
{
  n = 10;
}

int main()
{
   int num = 4;
   myFunc(num);
   cout << num;
}

Flag this Question

Question 6 10 pts

What is the output of the following code?

int myFunc(int n)
{
  n = 10;
  return n;
}

int main()
{
   int n = 4;
   cout << myFunc(n);
}

Flag this Question

Question 7 10 pts

(True or False) The following is an infinite loop.

int main()
{
bool flag = false;
int num = 0;
while(!flag);
{
   cin >> num;
   if(num == -1) {
     flag = true;
   }
}
return 0;
}

Group of answer choices

True

False

Flag this Question

Question 8 10 pts

What would the output of the following code be?

for(int i = 0; i < 5; ++i)
{
    for(int j = 0; j < i; ++j)
    {
      cout << j;
    }
}

Flag this Question

Question 9 10 pts

Given the following input:

12.34.a

What would the output of the following be?

int i;
float j;
char ch;
cin >> i >> j >> ch
cout << i << " " << j << " " << ch;

Solutions

Expert Solution

Q1

Answer: False

The function's return type is void. But the function is returning a value. This will lead to an error, and thus, the program will not compile.

Q2

Answer: True

Because the length of "before" is greater than the length of "back". Therefore, the if condition of the code gets true value from the isGreater function, thus leading to execute the if part of the code.

Q3

Output: 0123401234

The outer loop will run 2 times for i=1 and i=2.

For each iteration of outer loop the inner loop will run 5 times with the value of j being 0,1,2,3,4.

Thus the output will be 0123401234.

Q4

Output: 4

The line

myFunc(num);

has no effect on the value of the variable num. Because the value is passed as pass by value. Therefore, the changes made will have scope only till the function myFunc exists. After that the previous value of num will continue.

Therefore, the value of num remains 4 and thus 4 is printed as output.

Q5

Output: 10

As the value is passed as pass by reference that is the address of the variable num is passed, the modifications made in the function will be having effect in the main function also.

Therefore, the value of num becomes 10 and is displayed as output.

Q6

Output: 10

The function myFunc is returning value of n=10, which will be displayed by the cout function in the main.

Therefore, 10 will be the output.

Q7

True

The while statement has been ended with a semicolon thus resulting the loop to fall in infinite state.

Q8

Output: 0010120123

i=0

j=0

output: 0

i=1

j=0

output: 00

i=1

j=1

output: 001

i=2

j=0

output: 0010

i=2

j=1

output: 00101

i=2

j=2

output: 001012

i=3

j=0

output: 0010120

i=3

j=1

output: 00101201

i=3

j=2

output: 001012012

i=3

j=3

output: 0010120123

Q9

The given program has an error in the line

cin >> i >> j >> ch

The line misses a semicolon in the end.

Had the line been

cin >> i >> j >> ch;

then the output would had been

12 0.34 .


Related Solutions

What is the ouput of the following code? void loop(int num) { for(int i = 1;...
What is the ouput of the following code? void loop(int num) { for(int i = 1; i < num; ++i) { for(int j = 0; j < 5; ++j) { cout << j; } } } int main() { loop(3); return 0; }
public boolean isPrime(int num){ // return true if the number is prime, false otherwise } public...
public boolean isPrime(int num){ // return true if the number is prime, false otherwise } public boolean isOdd(int num){ // return true if the number is odd, false otherwise } public String reverseMyString(String input){ // using a loop, reverse a string // i.e. input = "hello", reversed answer: "olleh" // i.e. input = "bye", reversed answer: "eyb" } public int pow(int base, int power){ // using for loop, calculate base raised to power // i.e. base = 2, power =...
/*Use recursion in the function: void getDigit( int num) /* Try this program and implement the...
/*Use recursion in the function: void getDigit( int num) /* Try this program and implement the function void getDigit( intnum) so that it displays the digits in a given number. For example, the number, 1234 consist of 1, 2, 3 and 4. This is an exercise to demonstate the use of a recursive function and the use of recusrion in C++ */ #include #include using namespace std; int main() {      int num;      cout <<"Enter an integer: ";     ...
What will the following code segments print on the screen? int num = 3; switch (num){...
What will the following code segments print on the screen? int num = 3; switch (num){             case 1:                         System.out.println(“Spring”); case 2:                         System.out.println(“Summer”); case 3:                         System.out.println(“Autumn”); case 4:                         System.out.println(“Winter”); }
(JAVA) InvertArrangement +invert(int[] a) : int [] +print(int[] a) : void Example 1: the invert method...
(JAVA) InvertArrangement +invert(int[] a) : int [] +print(int[] a) : void Example 1: the invert method receives the following arrangement: [1,2,3,4,5] The invert method returns the array [5,4,3,2,1] Example 2: the print method receives the following array: [5,4,3,2,1] The print method prints: 5,4,3,2,1 (data separated by commas). TIP: for the print method use System.out.print () without the "ln".
What is time Complexity each of the following function? 1- void function(int n) {             for (int...
What is time Complexity each of the following function? 1- void function(int n) {             for (int i=n/2; i<=n; i++)                           for (int j=1; j<=n/2; j++)                                     for (int k=1; k<=n; k = k * 2)                                                 print ”Hello”; } 2- void function(int n) {             for (int i=n/2; i<=n; i++)                           for (int j=1; j<=n; j = 2 * j)                                     for (int k=1; k<=n; k = k * 2)                                                 print ”Hello”; } 3- void function(int n) {             for (int i=1; i<=n; i++)                           for (int j=1;...
Instructions (in C++): 1 ) Use a void function to print the following message (should be...
Instructions (in C++): 1 ) Use a void function to print the following message (should be in welcome function) Welcome to the Event Scheduling program 2 ) create 3 int arrays with 3 positions (one array for days one array for moths and one array for years) (should be in main) 3 ) Create a file that contains the following (you can just create the file or write the file in the program) 1 / 26 / 2021 12 /...
Q1: Given the following code, what is returned by tq(4)? int tq(int num){ if (num ==...
Q1: Given the following code, what is returned by tq(4)? int tq(int num){ if (num == 0) return 0; else if (num > 100) return -1; else     return num + tq( num – 1 ); } Group of answer choices: 0 4 -1 10 Q2: Given that values is of type LLNode<Integer> and references a linked list (non-empty) of Integer objects, what does the following code do if invoked as mystery(values)? int mystery(LLNode<Integer> list) {    if (list.getLink() ==...
#include #include #include int main(void) { int feof(FILE *stdin); int i, num; int binary[10]; char input[10];...
#include #include #include int main(void) { int feof(FILE *stdin); int i, num; int binary[10]; char input[10]; printf("Starting the CPSC 1011 Decimal to Binary Converter!\n"); while(1) {    i=0;    printf("\nPlease enter a positive whole number (or EOF to quit): ");    scanf("%s", input); // user inputs value as a string for separate values    if(strcmp(input,"")==0) {        printf("\n\tThank you for using the CPSC 1011 Decimal to Binary Generator.\nGoodbye!\n\n");    return(0); } num=atoi(input); if (num<=0) {    printf("\n\tSorry, that was...
#include<iostream> using namespace std; class point{ private: int x; int y; public: void print()const; void setf(int,...
#include<iostream> using namespace std; class point{ private: int x; int y; public: void print()const; void setf(int, int); }; class line{ private: point ps; point pe; public: void print()const; void setf(int, int, int, int); }; class rectangle{ private: line length[2]; line breadth[2]; public: void print()const; void setf(int, int, int, int, int, int, int, int); }; int main(){ rectangle r1; r1.setf(3,4,5,6, 7, 8, 9, 10); r1.print(); system("pause"); return 0; } a. Write function implementation of rectangle, line and point. b. What is...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT