Self Studies

Programming and Data Structures Test 2

Result Self Studies

Programming and Data Structures Test 2
  • Score

    -

    out of -
  • Rank

    -

    out of -
TIME Taken - -
Self Studies
Weekly Quiz Competition
  • Question 1
    2 / -0.33

    How do you instantiate an array in Java?

    Solution

    Note that int arr[]; is declaration whereas int arr[] = new int[3]; is to instantiate an array.

  • Question 2
    2 / -0.33

    What is the output of the following Java code?
    public class array
    {
        public static void main(String args[])
        {
            int []arr = {1,2,3,4,5};
            System.out.println(arr[2]);
            System.out.println(arr[4]);
        }
    }

    Solution

    Array indexing starts from 0.

  • Question 3
    2 / -0.33

    When does the ArrayIndexOutOfBoundsException occur?

    Solution

    ArrayIndexOutOfBoundsException is a run-time exception and the compilation is error-free.

  • Question 4
    2 / -0.33

    What is the number of  binary search trees possible with 9 distinct keys?

    Solution

    Data:

    number of distinct keys = n = 9

    Formula:

    Number of binary search trees = \(^{2n}C_n \over n+1\)

    Calculation:

    Number of binary search trees = \(^{18}C_9\over 10\) = 4862

  • Question 5
    2 / -0.33

    What are the advantages of arrays?

    Solution

    Arrays store elements of the same data type and present in continuous memory locations.

  • Question 6
    2 / -0.33

    Assuming int is of 4bytes, what is the size of int arr[15];?

    Solution

    Since there are 15 int elements and each int is of 4bytes, we get 15*4 = 60bytes.

  • Question 7
    2 / -0.33

    A program X reads in 300 integers in the range [0..100] representing the %attendance of 300 students. It then prints the frequency of each %attendance above 25. What would be the best way for X to store the frequencies?

    Solution

    Output is the frequency of each%attendance above 25.

    int result [75] //  

    To store frequencies of ages above 25. We can ignore ages below 25 so take an array of 75 numbers where a[0] corresponds to 26, a[1] corresponds to 27, …, a[74].

    Hence An array of 75 integers is enough.

  • Question 8
    2 / -0.33

    Elements in an array are accessed _____________

    Solution

    Elements in an array are accessed randomly. In Linked lists, elements are accessed sequentially.

  • Question 9
    2 / -0.33

    How do you initialize an array in C?

    Solution

    This is the syntax to initialize an array in C.

  • Question 10
    2 / -0.33

    If a queue is implemented using two stacks. In enqueue operation, all the elements are pushed from the first stack to the second stack. In dequeue operation pop an element from 1st stack.

    Which of the following is/are true?
    Solution

    Enqueue:

    While 1st  stack is not empty, push everything from 1st to 2nd stack.

    Push an element to 1st stack.

    Push everything back to 1st stack.

    Here time complexity will be O(n)

    Dequeue:

    while 1st stack is not empty then Pop an item from stack1 and return it

    Here time complexity will be O(1)

    Therefore option 1 and 3 are correct.

  • Question 11
    2 / -0.33

    A function f defined on stack of integer satisfies the following properties f(empty) = 0 and f (push (S, i)) = max (f(s), 0) + i for all stacks S and integer i.

    After pushing integers 2, -3, 2, -1, 2 in order from bottom to top. f(s) returns the top of the stack. What is f(s)?
    Solution

    Initially S is empty,

    ∴ f(s) = 0

    f(push(S, 2)) = 2

    f(push(S, -3)) = max (2, 0) – 3 = -1

    f(push (S, 2)) = max (-1, 0) + 2 = 2

    f(push (S, -1)) = max (2, 0) – 1= 1

    f(push (S, 2)) = max (1, 0) + 2 = 3
  • Question 12
    2 / -0.33

    What is the return output of the following C program segment?

    char c= '2';

     switch(c)

    {

    case '1': printf("C program, ");           

    case '2':

    case '3': printf("Java program, ");

    case '4' :

    case '5': break;

    default: printf("No program");

    }

    return 0;

    }
    Solution

    switch('2')

    control will come to case '2'

    since case '2': doesn't have break statement to will go to case '3'

    In case '3': Java program, is printed

    since case '3': doesn't have break statement to will go to case '4'

    From case '4' to it will to case '5' break will execute and the switch will terminate.

    Output: Java program,

  • Question 13
    2 / -0.33

    Which of the following is the correct way to declare a multidimensional array in Java?

    Solution

    The syntax to declare multidimensional array in java is either int[][] arr; or int arr[][];

  • Question 14
    2 / -0.33
    Which of the following is/are true with respect to stacks and queues where where front points to the index where deletion is done and the rear points to the index where insertion is done in queue?
    Solution

    Concept:
    The breadth-first search algorithm uses a queue data structure while the depth-first search algorithm uses a stack data structure in its implementation.

    The necessary condition to detect if a circular queue of capacity (n-1) is full is (rear+1) mod n = front 

    The time complexity of converting a fully parenthesized infix expression to postfix expression is O(n), that is, traverse the equation

    Conclusion:

    Options1, 2 and 4 are correct

  • Question 15
    2 / -0.33

    What is the output of the following Java code?

    public class array
    {
        public static void main(String args[])
        {
            int []arr = {1,2,3,4,5};
            System.out.println(arr[5]);
        }
    }

    Solution

    Trying to access an element beyond the limits of an array gives ArrayIndexOutOfBoundsException.

  • Question 16
    2 / -0.33

    Which of the following is not a logical operator?

    Solution

    &&- Logical AND 
    !- Logical NOT 
    ||- Logical OR 
    |- Bitwise OR(used in bitwise manipulations)

  • Question 17
    2 / -0.33

    Which of the following is true about return type of functions in C?

    Solution

    In C, functions can return any type except arrays and functions. We can get around this limitation by returning pointer to array or pointer to function.

  • Question 18
    2 / -0.33

    Consider the following C declaration

    struct { 
        short s[5];
        union { 
             float y; 
             long z; 
        }u; 
    } t;

    Assume that objects of the type short, float and long occupy 2 bytes, 4 bytes and 8 bytes, respectively. The memory requirement for variable t, ignoring alignment considerations, is

    Solution

    Short array s[5] will take 10 bytes as size of short is 2 bytes.

    When we declare a union, memory allocated for the union is equal to memory needed for the largest member of it, and all members share this same memory space. Since u is a union, memory allocated to u will be max of float y(4 bytes) and long z(8 bytes). So, total size will be 18 bytes (10 + 8).

  • Question 19
    2 / -0.33

    #include <stdio.h>
    int main()
    {
        int i = 3;
        printf("%d", (++i)++);
        return 0;
    }

    What is the output of the above program?

    Solution

    In C, prefix and postfix operators need l-value to perform operation and return r-value. The expression (++i)++ when executed increments the value of variable i(i is a l-value) and returns r-value. The compiler generates the error(l-value required) when it tries to post-incremeny the value of a r-value.

  • Question 20
    2 / -0.33

    Consider the following C program

    main ( )

    {

    int N, x;

    scanf (“% d”, &N);

    x = 0;

    while (N > 0)

    {

    x = x + 1 – N % 2;

    N / = 2;

    }

    printf(“% d”, x)

    }

    What is the sum of the smallest three values of N for which the value printed is 5?

    Solution

    Take N = 32

    N = 32

    while (TRUE)

    x = 0 + 1 – 0

    N = 16

    while (TRUE)

    x = 1 + 1 – 0

    N = 8

    while (TRUE)

    x = 2 + 1 – 0

    N = 4

    while (TRUE)

    x = 3 + 1 – 0

    N = 2

    while (TRUE)

    x = 4 + 1 – 0

    N = 1

    while (TRUE)

    x = 5 + 1 – 1 = 5

    N = 0

    while (FALSE)

    Output will be same for 65 and 66 as well.

    For N = 32 it will print '5'

    For N = 65 it will print '5'

    For N = 66 it will print '5'

    Hence, the sum of smallest three values of N = 32 + 65 + 66 = 163

  • Question 21
    2 / -0.33

    Find the Output ?

    #include<stdio.h>

    int main() 

      int x, y = 5, z = 5; 

      x = y == z; 

      printf("%d", x); 

      getchar(); 

      return 0; 

    }

    Solution

    1. Variable Declaration and Initialization:

      c

      Copy code

      int x, y = 5, z = 5;

      • x, y, and z are declared as integers.
      • y and z are initialized to 5.
    2. Assignment and Comparison:

      c

      Copy code

      x = y == z;

      • Here, y == z is a comparison operation (y equals z).
      • Since y and z both hold the value 5, y == z evaluates to true, which is represented as 1 in C.
    3. Assignment Result:

      • The result of y == z (which is 1) is assigned to x.
      • Therefore, x now holds the value 1.
    4. Printing the Result:

      c

      Copy code

      printf("%d", x);

      • This line prints the value of x.
      • x currently holds 1, so 1 is printed to the console.
    5. Final Steps:

      c

      Copy code

      getchar(); return 0;

      • getchar() is used to wait for a character input (typically used to keep the console window open).
      • return 0; indicates successful completion of the main function.

    Therefore, when you run this program, it will output:

    Copy code

    1

  • Question 22
    2 / -0.33

    Which file is generated after pre-processing of a C program?

    Solution

    After the pre-processing of a C program, a .i file is generated which is passed to the compiler for compilation.

  • Question 23
    2 / -0.33

    Which of the following is not a storage class specifier in C?

    Solution

    volatile is not a storage class specifier. volatile and const are type qualifiers.

  • Question 24
    2 / -0.33

    The following postfix expression with single digit operands in evaluated using a stack

    4 2 5 ^ * 14 7 / - 7 5 * +

    Note that ^ is exponentiation operator, * is multiplication operator, / is division operator, + is addition operator and – is subtraction operator
    Solution

    4 2 5 ^ * 14 7 / - 7 5 * +

    Push: 9, 2 and 5

    4

    2

    5

     

    Pop: 2 and 5

    Perform operation: 2 ^ 5 = 32

    Push: 32

    4

    32

     

     

    Pop: 32 and 4

    Perform operation: 4 × 32 = 128

    Push: 128

    Also push: 14 and 7

    128

    14

    7

     

    Pop: 7 and 14

    Perform operation: 14 ÷ 7 = 2

    Push: 2

    128

    2

     

     

    Pop: 128 and 2

    Perform operation: 128 - 2 = 126

    Push: 126

    Also push: 7 and 5

    126

    7

    5

     

    Pop: 5 and 7

    Perform operation: 7 × 5 = 35

    Push: 35

    126

    35

     

     

    Pop: 35 and 126

    Perform operation: 126 + 35 = 161

    Push: 161

    161

     

     

     

    After evaluating: 161 is present onto the stack

    Hence 161 is the answer for the given postfix operation.
  • Question 25
    2 / -0.33

    In C, parameters are always

    Solution

    In C, function parameters are always passed by value. Pass-by-reference is simulated in C by explicitly passing pointer values.

Self Studies
User
Question Analysis
  • Correct -

  • Wrong -

  • Skipped -

My Perfomance
  • Score

    -

    out of -
  • Rank

    -

    out of -
Re-Attempt Weekly Quiz Competition
Self Studies Get latest Exam Updates
& Study Material Alerts!
No, Thanks
Self Studies
Click on Allow to receive notifications
Allow Notification
Self Studies
Self Studies Self Studies
To enable notifications follow this 2 steps:
  • First Click on Secure Icon Self Studies
  • Second click on the toggle icon
Allow Notification
Get latest Exam Updates & FREE Study Material Alerts!
Self Studies ×
Open Now