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...
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; }
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 <iomanip> using namespace std; int main() {     int studentid, numberreverse[20], count =...
#include <iostream> #include <iomanip> using namespace std; int main() {     int studentid, numberreverse[20], count = 0, maximum = 0, minimum = 0;     cout << "Enter your student ID number: ";     cin >> studentid;     cout << "Student ID Number = " << studentid << endl;     while (studentid != 0)     {          numberreverse[count] = studentid % 10;          if (count == 0)          {              minimum = numberreverse[count];              maximum = minimum;          }          else...
#include <iostream> #include <fstream> #include <string> using namespace std; const int QUIZSIZE = 10; const int...
#include <iostream> #include <fstream> #include <string> using namespace std; const int QUIZSIZE = 10; const int LABSIZE = 10; const int PROJSIZE = 3; const int EXAMSIZE = 3; float getAverage(float arr[], int size) { float total = 0; for (int i = 0; i < size; i++) { total += arr[i]; } return total/size; } // the following main function do.... int main() { ifstream dataIn; string headingLine; string firstName, lastName; float quiz[QUIZSIZE]; float lab[LABSIZE]; float project[PROJSIZE]; float midExam[EXAMSIZE];...
// This program demonstrates a Binary Search //PLACE YOUR NAME HERE #include<iostream> using namespace std; int...
// This program demonstrates a Binary Search //PLACE YOUR NAME HERE #include<iostream> using namespace std; int binarySearch(int [], int, int);  // function prototype const int SIZE = 16; int main() { int found, value; int array[] = {34,19,19,18,17,13,12,12,12,11,9,5,3,2,2,0}; // array to be searched cout << "Enter an integer to search for:" << endl; cin >> value; found = binarySearch(array, SIZE, value); //function call to perform the binary search   //on array looking for an occurrence of value if (found == -1) cout...
#include <iostream> using namespace std; const int DECLARED_SIZE = 10; void fillArray(int a[], int size, int&...
#include <iostream> using namespace std; const int DECLARED_SIZE = 10; void fillArray(int a[], int size, int& numberUsed) { cout << "Enter up to " << size << " nonnegative whole numbers.\n" << "Mark the end of the list with a negative number.\n"; int next, index = 0; cin >> next; while ((next >= 0) && (index < size)) { a[index] = next; index++; cin >> next; } numberUsed = index; } int search(const int a[], int numberUsed, int target) {...
#include <iostream> #include <string> #include <ctime> using namespace std; void displayArray(double * items, int start, int...
#include <iostream> #include <string> #include <ctime> using namespace std; void displayArray(double * items, int start, int end) { for (int i = start; i <= end; i++) cout << items[i] << " "; cout << endl; } //The legendary "Blaze Sort" algorithm. //Sorts the specified portion of the array between index start and end (inclusive) //Hmmm... how fast is it? /* void blazeSort(double * items, int start, int end) { if (end - start > 0) { int p =...
write the algorithm for this the code?!. #include<iostream> using namespace std; #include<string.h> int main() { char...
write the algorithm for this the code?!. #include<iostream> using namespace std; #include<string.h> int main() { char plain[50], cipher[50]="", decrypt[50]=""; int subkeys[50], len;       cout<<"Enter the plain text:"<<endl; cin>>plain;    cout<<"Enter the first subkey:"<<endl; cin>>subkeys[0];    _strupr(plain);    len = strlen(plain);    /**********Find the subkeys**************/    for(int i=1; i<len; i++) { if ((plain[i-1]>='A') && (plain[i-1]<='Z')) { subkeys[i] = plain[i-1]-65; } }    /****************ENCRYPTION***************/       for(int i=0; i<len; i++) { if ((plain[i]>='A') && (plain[i]<='Z')) {    cipher[i] = (((plain[i]-65)+subkeys[i])%26)+65; }...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT