Q.1. What is the output of this program?
#include <stdio.h>
using namespace std;
int main()
{
int a = 5, b=10, c = 15;
int arr[3] = {&a, &b, &c};
cout << *arr[*arr[1] - 8];
return 0;
}
(1) garbage value
(2) compile time error
(3) 18
(4) 15
Explanation:
1. The program declares three variables: `a`, `b`, and `c`, with respective integer values of 5, 10, and 15.
2. An array called `arr` with a size of 3 is declared to hold integers, but instead, memory addresses are assigned to its elements using the unary `&` operator. This is incorrect, as the array should hold integer values directly.
3. The statement `*arr[*arr[1] - 8]` attempts to access the element of `arr` at index `*arr[1] - 8`, which involves dereferencing an element of `arr`. However, since `arr` contains memory addresses instead of integers, this results in undefined behavior.
4. The resulting value from the incorrect expression is printed using `cout`. However, since the program has undefined behavior, the output cannot be predicted.
5. Finally, the program returns 0, indicating successful execution.
In summary, the code has several issues and will result in undefined behavior due to incorrect array initialization and improper use of memory addresses in the expression.
Q.2. What is the output of this program?
#include <iostream>
#include <deque>
using namespace std;
int main ()
{
deque<int> mydeque (5);
deque<int>::reverse_iterator rit = mydeque.rbegin( );
int i = 0
for (rit = mydeque.rbegin( ); rit!= mydeque.rend( ); ++rit)
*rit = ++i;
for (deque<int> :: iterator it = mydeque.begin( ):
it != mydeque.end( ); ++it)
cout << '' << *it;
return 0;
}
(1) 1234
(2) 54321
(3) 43210
(4) 12345
Explanation:
The deque mydeque is created with a size of 5, so it initially contains 5 elements, all with the default value of 0.
The reverse iterator rit is initialized to the reverse beginning of mydeque.
The first for loop iterates over mydeque using the reverse iterator rit. The loop starts from the reverse beginning and assigns values to the elements in ascending order from 1 to 5.
After the first loop, the values of mydeque will be {5, 4, 3, 2, 1}.
The second for loop uses a regular iterator it to iterate over mydeque from the beginning to the end, printing the values of the elements.
The output of the second loop will be " 5 4 3 2 1", as each element is printed separated by a space.
Q.3. What is the output of this C code?
#include <stdio.h>
int main()
{
int a[4] = {1, 2, 3, 4};
int b[4] = {1, 2, 3, 4};
int n = &b[3] - &a[2];
printf("%d\n", n);
}
(1) -3
(2) 4
(3) 5
(4) Compile time error
Explanation:
The code includes the necessary header file stdio.h to use input/output functions in C.
The main function is defined as the entry point of the program.
Two integer arrays a and b are declared, each with four elements initialized to the values 1, 2, 3, and 4.
The expression &b[3] - &a[2] is assigned to the integer variable n. This expression calculates the difference between the memory addresses of b[3] and a[2].
The printf function is used to print the value of n as an integer.
The program returns 0, indicating successful execution.
Q.4. What is the output of this C code?
#include <stdio.h>
int main()
{
int ary[2][3];
foo(ary);
}
void foo(int *ary[ ])
{
Int i = 10, j =2,k;
ary[0] = &i;
ary[1] = &j;
*ary[0] = 2;
for (k = 0;k < 2; k++)
printf("%d\n", *ary[k]);
}
(1) Compile time error
(2) 22
(3) Undefined behaviour
(4) 102
Explanation:
The foo function takes an array of integer pointers as a parameter.
Inside foo, ary[0] is assigned the memory address of i and ary[1] is assigned the memory address of j.
*ary[0] = 2 updates the value of i to 2 since ary[0] contains the memory address of i.
The for loop iterates over the ary array and prints the values stored in the memory locations pointed to by ary[k]. In this case, it prints 2 and 2 since ary[0] and ary[1] both point to the updated value of i.
In the main function, the foo function is called with the ary array as an argument.
The output of the program will be:
2
2
This is because foo modifies the value of i through the pointer ary[0], and the modified value is printed twice in the for loop.
Q.5.What is the output of this C code?
#include <stdio.h>
int main()
{
char *str = "hello world";
char strary[] = "hello world”;
printf("%d %d\n", strlen(str), strlen(strary));
return 0;
}
(1) x 11 where x can be any positive integer
(2) 1211
(3) 1111
(4) 1112
Explanation:
The pointer str points to a string literal "hello world".
The array strary contains a copy of the string literal "hello world".
The strlen function is used to calculate the length of a string, which is the number of characters in the string excluding the null-terminating character.
In the printf statement, strlen(str) returns the length of the string "hello world" pointed to by str, which is 11.
strlen(strary) returns the length of the string "hello world" stored in the strary array, which is also 11.
Therefore, the printf statement prints 11 11.
Q.1 Which of these class holds a collection of static methods and variables?
(1) Runtime
(2) System
(3) Process
(4) Void
Q.2. What is the scope of an external variable?
(1) Whole source file in which it is defined
(2) From the point of declaration to the end of the file in which it is defined
(3) Any source file in a program
(4) From the point of declaration to the end of the file being compiled
Q.3. What is the use of middle parameter in rotate method?
(1) Marks the elements in a sequence
(2) Marks the beginning of a sequence
(3) None of the mentioned
(4) Marks the ending of a sequence
Q.4. Identify the type of the variables.
typedef char* CHAR;
CHAR p,q;
(1) Unknown
(2) char*
(3) CHAR
(4) char
Q.5. Which of three sizes of floating point types should be used when extended precision is required?
(1) Float
(2) long double
(3) Double
(4) extended float