In: Computer Science
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 |
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