Question

In: Computer Science

Question 1 Given the program below, what are the errors. #include <iostream> using namespace std; int...

Question 1

Given the program below, what are the errors.

#include <iostream>

using namespace std;

int main() {

int ind_square(int &);

int x, *p;

x=15;

p = &x;

ind_square(*p);

}

int ind_square(int &p)

{

*p = *p **p;

}

Question 1 options:

C. No return for non-void function

A. Indirection for the variables in ind_square requires pointer operand

A and B

B. Invalided use of address (&) symbol as a parameter for ind_squared

A and C

Question 2

Which statement is false about what Data Types defines

Question 2 options:

What value a variable will hold?

What values a variable cannot hold?

How much memory will be reserved for the variable?

How the program will use the data type?

Question 3

Use the correct operator to concatenate two strings:

string firstName = "John ";

string lastName = "Doe";

cout << firstName

lastName;

Question 4

The delete operator deallocates memory that was allocated.

Question 4 options:

True
False

Question 5

The address operator is not needed to assign an arrays address to a pointer.

Question 5 options:

True
False

Question 6

Given the structure, assignment and statement below. Which of the following statements about accessing the members of structure is incorrect?
struct Employee{
    string emp_id;
    string emp_name;
    string emp_sex;
};

Employee Emp, *pEmp;
pEmp=&Emp;

Question 6 options:

Emp->emp_id;

pEmp->emp_id;

Emp.emp_id;

Question 7

In the program below, what are the errors?

#include <iostream>

using namespace std;

int main() {

int ivar;

int *iptr = &ivar;

ivar =10;

cout << "The value of ivar is: "<< iptr<<endl;

int nvar, nptr = &nvar;

float fvar;

int *lptr = &fvar;

int nums[50], *pptr = nums;

cout << "The address of nums is: "<< *pptr<<endl;


int *zptr = &ivar;

cout << "The value of zptr is: "<< *zptr<<endl;

int zvar;

cout << "The value of zvar is: "<< zvar<<endl;

}//

Question 7 options:

A. The first cout prints the address of iptr

B. lptr is an integer not a float

C. nvar is not a pointer

D. The second cout doesn’t print the assigned the address of nums

a and b

a and c

c and b

a, b, c and d

Question 8

Fill in the missing part to create a variable of type string called “city” and assign it the value of “Chicago”

;

Question 9

Given the code snippet below, what prints?

int main()

{

    int a[5] = {1,2,3,4,5};

    int *ptr = (int*)(&a+1);

    cout << *(a+1)<< *(ptr-5);

    return 0;

}

Question 9 options:

2 1

compiler error

runtime error

2 5

Question 10

To compare or print their value a pointer is pointing to, use the “ &” operator.

Question 10 options:

True
False

Solutions

Expert Solution

1) For the given code there are two error... one is in the function ind_square(). here

*p = *p **p; statement is weong... it should be *p = *p *(*p); there must be a parenthesis...

another one is the invalid use of & symbol....

Note: No return for non-void function will give a warning, but no error is there

so the correct option is A and B

2) the false statement is "How the program will use the data type"... How the program will use the data type? is totally depends on the user/programer...

For the other option, variables defines

What value a variable will hold : it is true

What values a variable cannot hold : it is also true

How much memory will be reserved for the variable : it is also true

3) the correct way is

string firstName = "John ";

string lastName = "Doe";

cout << firstName<<lastName;

"<<" operator is used to concatinate two string

4) the statement is True...

the syntax of delete keyword is

delete pointer_variable;

5) the given statement is true...

the syntax of assigning an array address to a pointer is

int arr[4] = {1,2,3,4};

int *ptr = arr; so there is no need of '&' for assigning.

6) The wrong option is Emp->emp_id;

Because Emp is a structure variable, not a pointer .. so we cannot use '->' operator to a veriable... it can only be used for pointers... the statement should be " Emp.emp_id; " so a dot(.) operator is needed instead of '->'

the other options are correct because '->' is used for pointer and ' . ' is used for variable..

7) For this problem, all the statements are correct...

The first cout prints the address of iptr : true because iptr is a pointer

lptr is an integer not a float : true because int pointer cannot store float variable address

nvar is not a pointer : true because nvar is an integer variable

The second cout doesn’t print the assigned the address of nums: true because it will print a garbage value

so all are correct... i.e. a,b,c and d

8) the syntax is Datatype variable_name = value;

so answer is, string city = " Chicago ";

9) The correct answer is " 2 1 "

because, *ptr holds the next address of the array a.

now, *(a+1) means value at a[1]. it will print 2

and *(p-5) means value at p[-5] which is equivalent to a[0].. so it will print 1

10) to compare, we use <,>,<=,>=,== operators and for printing the value a pointer is pointing to, we use ' * ' operator.

eg. cout<<*p; it will print the value which is at the address p.

so the statement is False


Related Solutions

What is the output of the following program? #include <iostream> using namespace std; void showDouble(int); //Function...
What is the output of the following program? #include <iostream> using namespace std; void showDouble(int); //Function prototype int main() { int num; for (num = 0; num < 10; num++) showDouble(num); return 0; } // Definition of function showDouble void showDouble(int value) { cout << value << ‘\t’ << (value * 2) << endl; } Please do the following Program and hand in the code and sample runs. Write a program using the following function prototypes: double getLength(); double getWidth();...
#include <iostream> using namespace std; int main() {     int hour;     int min;     for (hour = 1;...
#include <iostream> using namespace std; int main() {     int hour;     int min;     for (hour = 1; hour <= 12; hour++)     {         for (min = 0; min <= 59; min++)         {             cout << hour << ":" << min << "AM" << endl;         }     }       return 0; } 1.      Type in the above program as time.cpp. Add a comment to include your name and date. Compile and run. 2.      What is the bug or logic error in the above program? Add the...
Add File I/O to the voting program below #include<iostream> using namespace std; int main() {int choice;...
Add File I/O to the voting program below #include<iostream> using namespace std; int main() {int choice; int biden = 0 , trump = 0 , bugs = 0 ; int vc = 0 ; do { cout<<"\n\n\nEVOTE\n-----" <<"\n1.Joe Biden" <<"\n2.Donald Trump" <<"\n3.Bugs Bunny" // 4. Print current tally [hidden admin option] // 5. Print audit trail [hidden admin option] // 6. mess with the vote [hidden hacker option] E.C. // 7. END THE ELECTION <<"\n\n Your selection? "; cin >>...
complete the program #include <cstdlib> #include <iostream> #include <iomanip> using namespace std; int main(int argc, char**...
complete the program #include <cstdlib> #include <iostream> #include <iomanip> using namespace std; int main(int argc, char** argv) { int number, sum, count; // Write a while loop that reads a number from the user and loop // until the number is divisible by 7 cout << "What is the number? "; cin >> number; while ( ... ) { ... } cout << number << " is divisible by 7!! << endl << endl; // Write a for loop that...
#include <cstring> #include <stdio.h> #include <iostream> using namespace std; int main() {        const int...
#include <cstring> #include <stdio.h> #include <iostream> using namespace std; int main() {        const int SIZE = 20;     char str[SIZE];     char str1[SIZE];     int n;     int k =1;        printf("Enter a word: \n");     fgets(str,SIZE,stdin);     printf("Enter another word: \n");     fgets(str1,SIZE,stdin);        if (str1[strlen(str1) - 1] == '\n')     {         str1[strlen(str1)-1] = '\0';     }     if (str[strlen(str) - 1] == '\n')     {         str[strlen(str)-1] = '\0';     }      ...
What would the following program output? #include <iostream> using namespace std; int main() { char alpha...
What would the following program output? #include <iostream> using namespace std; int main() { char alpha = 'A'; for(int i = 0; i < 13; i++){ for(int j = 0; j < 2; j++){ cout << alpha; alpha++; } } cout << endl; return 0; }
Complete the following program #include<iostream> #include<iomanip> #include<fstream> using namespace std; int main() { // I -...
Complete the following program #include<iostream> #include<iomanip> #include<fstream> using namespace std; int main() { // I - Declaring a five by five array /* II - Read data from data.txt and use them to create the matrix in the previous step*/    // III - Count and print the number of even integers in the matrix. /* IV - Calculate and print the sum of all integers in the columns with an even index value. Please note the column index begins...
C++ Given Code: #include <iostream> #include <string> using namespace std; int main() { //declare variables to...
C++ Given Code: #include <iostream> #include <string> using namespace std; int main() { //declare variables to store user input bool cont = true; //implement a loop so that it will continue asking until the user provides a positive integer // the following provides ONLY part of the loop body, which you should complete { cout <<"How many words are in your message? \n"; cout <<"Enter value: "; // get user input integer here    cout <<"\nInvalid value. Please Re-enter a...
How do i make this program accept strings? #include <iostream> #include <algorithm> using namespace std; int...
How do i make this program accept strings? #include <iostream> #include <algorithm> using namespace std; int binaryToOctal(char digits[], int a) { int number; if (digits[0] == '0') { if (digits[1] == '1') if (digits[2] == '0') return 2; //found "010" else return 3; //found "011" else if (digits[1] == '0') if (digits[2] == '1') return 1; // found "001" else return 0; //found "000" } else { if (digits[0] == '1') if (digits[1] == '1') if (digits[2] == '0') return...
#include <iostream> #include "lib.hpp" using namespace std; int main() {    // declare the bool bool...
#include <iostream> #include "lib.hpp" using namespace std; int main() {    // declare the bool bool a = true; bool b= true;    //Print the Conjunction function cout<<"\n\nConjunction Truth Table -"<<endl; cout<< "\nP\tQ\t(P∧Q)" <<endl; cout<< a <<"\t"<< b <<"\t"<< conjunction(a,b) <<endl; cout<< a <<"\t"<< !b <<"\t"<< conjunction(a,!b) <<endl; cout<< !a <<"\t"<< b <<"\t"<< conjunction(!a,b) <<endl; cout<< !a <<"\t"<< !b <<"\t"<< conjunction(!a,!b)<<endl;    //Print the Disjunction function cout<<"\n\nDisjunction Truth Table -"<<endl; cout<< "\nP\tQ\t(PVQ)" <<endl; cout<< a <<"\t"<< b <<"\t"<< disjunction(a,b) <<endl;...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT