Aspire's Library

A Place for Latest Exam wise Questions, Videos, Previous Year Papers,
Study Stuff for MCA Examinations

UGC NET Computer Science Previous Year Questions (PYQs)

UGC NET Computer Science C Programming Language PYQ


UGC NET Computer Science PYQ
What is x in the following program?
#include <stdio.h>

int main()
{
    typedef (*(*arrfptr[3])())[10];

    arrfptr x;

    return 0;
}





Go to Discussion

UGC NET Computer Science UGC NET PYQ UGC NET Computer Science UGC NET Computer Science June 2023 (Paper II) PYQ

Solution

The declaration:

typedef (*(*arrfptr[3])())[10];

means arrfptr is an array of 3 pointers to functions.

So,

arrfptr x;

declares x as an array of three function pointers.

UGC NET Computer Science PYQ
What is the output of following code?

main()
{
static float a[] = {13.24, 1.5, 4.5, 5.4, 3.5}

float *j, *k;

j = a;

k = a + 4

j = j * 2;

k = k/2;

printf("% f% f", *j, *k);

}





Go to Discussion

UGC NET Computer Science UGC NET PYQ UGC NET Computer Science UGC NET Computer Science June 2023 (Paper II) PYQ

Solution

In C, pointer multiplication and pointer division are not allowed.


Here,


j = j * 2;


and


k = k / 2;


are invalid operations.


UGC NET Computer Science PYQ

Consider the following pseudo-code fragment in which an invariant for the loop is $m \times x^k=p^n$ and $k\ge 0$.

Pre-conditions: $p\ge 1$ and $n\ge 0$

Assume that overflow never occurs.

int x = p;
int k = n;
int m = 1;

while (k != 0) {
if (k is odd) then m = m * x;
x = x * x;
k = floor(k / 2);
}

Which of the following must be true at the end of the while loop?






Go to Discussion

UGC NET Computer Science UGC NET PYQ UGC NET Computer Science UGC NET Computer Science June 2019 (Paper II) PYQ

Solution

Given loop invariant is

$m \times x^k=p^n$

At the end of the while loop, condition becomes false.

So,

$k=0$

Putting $k=0$ in invariant,

$m \times x^0=p^n$

Since,

$x^0=1$

Therefore,

$m=p^n$


UGC NET Computer Science PYQ

Consider the following C-code fragment running on a $32$-bit x86 machine:

typedef struct {
union {
unsigned char a;
unsigned short b;
} U;
unsigned char c;
} S;

S B[10];
S *p = address of B[4];
S *q = address of B[5];

p->U.b = 0x1234;

If $M$ is the value of $q-p$ and $N$ is the value of address of $(p->c)$ minus address of $p$, then $(M,N)$ is:






Go to Discussion

UGC NET Computer Science UGC NET PYQ UGC NET Computer Science UGC NET Computer Science June 2019 (Paper II) PYQ

Solution

Given that structure $S$ takes $32$ bits.

So,

$32\text{ bits}=4\text{ bytes}$

Now,

$p$ points to $B[4]$

$q$ points to $B[5]$

Since $p$ and $q$ are pointers of type $S$, pointer subtraction gives the difference in number of structures.

So,

$q-p=1$

Therefore,

$M=1$

Now, union $U$ contains $unsigned\ char\ a$ and $unsigned\ short\ b$.

Size of union $U$ is $2$ bytes.

After union $U$, variable $c$ is stored.

So, offset of $c$ from the beginning of structure is $2$ bytes.

Therefore,

$N=2$

Hence,

$(M,N)=(1,2)$


UGC NET Computer Science PYQ
The condition $num\ !=\ 65$ cannot be replaced by-





Go to Discussion

UGC NET Computer Science UGC NET PYQ UGC NET Computer Science UGC NET Computer Science Sep 2022 (Paper II) PYQ

Solution

The condition is:

$num\ !=\ 65$

It means $num$ is not equal to $65$.

Option (a):

$num>65\ ||\ num<65$

This is true when $num$ is not equal to $65$.

Option (b):

$!(num==65)$

This also means $num$ is not equal to $65$.

Option (c):

$num-65$

In C language, any non-zero value is treated as true.

So, this is also true when $num\neq 65$.

Option (d):

$!(num-65)$

This becomes true when $num-65=0$, that is $num=65$.

So, it is opposite of $num\ !=\ 65$.

UGC NET Computer Science PYQ
Pointers cannot be used to-





Go to Discussion

UGC NET Computer Science UGC NET PYQ UGC NET Computer Science UGC NET Computer Science Sep 2022 (Paper II) PYQ

Solution

Pointers are used to store addresses of variables.

They can be used to simulate call by reference.

They are also used to manipulate dynamic data structures like linked lists, trees, etc.

But pointers do not reference values directly. They access values indirectly through addresses.

UGC NET Computer Science PYQ

Consider the following C++ function $f()$:

unsigned int f(unsigned int n) {
unsigned int b = 0;
while (n) {
b = b + (n bitwise AND 1);
n = n shifted right by 1;
}
return b;
}

The function $f()$ returns the integer that represents the $P$ in the binary representation of positive integer $n$, where $P$ is:






Go to Discussion

UGC NET Computer Science UGC NET PYQ UGC NET Computer Science UGC NET Computer Science June 2019 (Paper II) PYQ

Solution

The expression $n$ bitwise AND $1$ checks the last bit of $n$.

If the last bit is $1$, then $b$ increases by $1$.

After that, $n$ is shifted right by one bit.

So, the loop checks each bit of $n$ one by one.

Therefore, $b$ counts the total number of $1$'s in the binary representation of $n$.


UGC NET Computer Science PYQ
The output of the following C++ Program is:
#include <stdio.h>
int main(void)
{
    int x, *p;
    x = 30;
    p = x;
    printf("%d", *p);
    return 0;
}





Go to Discussion

UGC NET Computer Science UGC NET PYQ UGC NET Computer Science UGC NET Computer Science June 2024 (Paper II) PYQ

Solution

p is a pointer variable and must store the address of x, written as:


p = &x;


But the program assigns:


p = x;


which means an integer is assigned to a pointer, causing a compilation error (type mismatch).


UGC NET Computer Science PYQ
Given

int arr[3][4][2] =
{
  {{2,4},{7,8},{3,4},{5,6}},
  {{7,6},{3,4},{5,3},{2,3}},
  {{8,9},{7,2},{3,4},{5,1}}
};

Which one of the following can be used to refer to element 1 in the above 3-d array?






Go to Discussion

UGC NET Computer Science UGC NET PYQ UGC NET Computer Science UGC NET Computer Science Dec 2022 Shift I (Paper II) PYQ

Solution

In C language, array indexing starts from $0$.

The element $1$ is present at:

$arr[2][3][1]$

Now,

$arr+2$ points to the third $2$-D block.

$*(arr+2)$ gives $arr[2]$.

$*(arr+2)+3$ points to $arr[2][3]$.

$*(*(arr+2)+3)$ gives $arr[2][3]$.

$*(*(arr+2)+3)+1$ points to $arr[2][3][1]$.

Therefore,

$*(*(*(arr+2)+3)+1)=arr[2][3][1]=1$


UGC NET Computer Science PYQ
What is the output of following C program?
#include <stdio.h>

main()
{
    int i, j, x = 0;

    for (i = 0; i < 5; ++i)
        for (j = 0; j < i; ++j)
        {
            x += (i + j - 1);
            break;
        }

    printf("%d", x);
}





Go to Discussion

UGC NET Computer Science UGC NET PYQ UGC NET Computer Science UGC NET Computer Science Dec 2019 (Paper II) PYQ

Solution

Outer loop runs for:

$i=0,1,2,3,4$

Inner loop runs only when $j<i$.

But inside inner loop, there is a $break$ statement, so inner loop executes only one time for each valid value of $i$.

For $i=0$:

$j<0$ is false, so inner loop does not execute.

For $i=1$:

$x=x+(1+0-1)=0+0=0$

For $i=2$:

$x=x+(2+0-1)=0+1=1$

For $i=3$:

$x=x+(3+0-1)=1+2=3$

For $i=4$:

$x=x+(4+0-1)=3+3=6$

So, final value of $x$ is:

$x=6$


UGC NET Computer Science PYQ
Which of the following statements about pointers in C are TRUE.

(A) Pointers can be used to access array elements
(B) Pointers can store the address of another pointer
(C) Pointers are automatically dereferenced in expression
(D) Pointers cannot be used to access structure members

Choose the correct answer from the options given below :





Go to Discussion

UGC NET Computer Science UGC NET PYQ UGC NET Computer Science UGC NET Computer Science August 2024 (Paper II) PYQ

Solution

(A) True — pointers can access array elements because arrays and pointers are closely related in C.

(B) True — a pointer can store the address of another pointer (pointer to pointer).

(C) False — pointers are not automatically dereferenced, we must use *.

(D) False — pointers can access structure members using ->.

Thus correct statements are A and B.

UGC NET Computer Science PYQ
Consider the following C program :
#include<stdio.h>
int main()
{
    int x[]={2,4,6,8,10};
    int a, b=0, *y = x + 4;

    for(a=0; a<5; a++)
    {
        b = b + (*y - a) - *(y - a);
    }

    printf("%d\n", b);
    return 0;
}
What will be the output of the above C program ?





Go to Discussion

UGC NET Computer Science UGC NET PYQ UGC NET Computer Science UGC NET Computer Science August 2024 (Paper II) PYQ

Solution

Pointer

$y = x + 4$

So

$*y = 10$

Loop runs $a=0$ to $4$

Expression:

$b = b + (*y - a) - *(y-a)$

Compute:

$a=0$ → $(10-0) - 10 = 0$
$a=1$ → $(10-1) - 8 = 1$
$a=2$ → $(10-2) - 6 = 2$
$a=3$ → $(10-3) - 4 = 3$
$a=4$ → $(10-4) - 2 = 4$

Sum

$b = 0+1+2+3+4 = 10$

UGC NET Computer Science PYQ
What will be the output of the following C code?
#include<stdio.h>
void main()
{
    int arr[5] = {10, 20, 30, 40, 50};
    int *p = (int*) (&arr + 1);
    printf("%d%d", *(arr + 1), *(p - 1));
}





Go to Discussion

UGC NET Computer Science UGC NET PYQ UGC NET Computer Science UGC NET Computer Science August 2024 (Paper II) PYQ

Solution


UGC NET Computer Science PYQ
Of the following, which is NOT a logical error?





Go to Discussion

UGC NET Computer Science UGC NET PYQ UGC NET Computer Science UGC NET Computer Science Sep 2022 (Paper II) PYQ

Solution

A logical error means the program runs, but it gives wrong output because the logic is incorrect.

Now check each option.

Option (a):

Using $'='$ instead of $'===' $ for comparison is a logical error.

Because $'='$ is used for assignment, while $'===' $ is used for strict comparison.

So, this may give wrong result.

Option (b):

Divide by zero is generally considered a run-time error.

The program may stop during execution.

Option (c):

Failing to initialize counter and total variables before loop execution is a logical error.

Because the program may use garbage or incorrect values and produce wrong output.

Option (d):

Using commas instead of required semicolons in a for loop header is a syntax error.

The program will not compile or run properly.

Since the question asks which is NOT a logical error, the best answer is syntax error.

UGC NET Computer Science PYQ

Arrange the following steps of file handling in C in the correct order :


(A) Close the file

(B) Read from or write to the file

(C) Open the file

(D) Check for error


Choose the correct answer from the options given below :






Go to Discussion

UGC NET Computer Science UGC NET PYQ UGC NET Computer Science UGC NET Computer Science August 2024 (Paper II) PYQ

Solution

Correct file handling sequence in C:


Open file


Check for error


Read / Write


Close file


Thus order:


(C) → (D) → (B) → (A)


UGC NET Computer Science PYQ
What does the following function $f()$ in C return?

int f(unsigned int N)
{
unsigned int counter = 0;

while (N > 0)
{
counter += N & 1;
N = N >> 1;
}

return counter == 1;
}





Go to Discussion

UGC NET Computer Science UGC NET PYQ UGC NET Computer Science UGC NET Computer Science Nov 2021 (Paper II) PYQ

Solution

Given function takes an unsigned integer $N$.

Inside the function:

$counter=0$

Now the loop runs while:

$N>0$

In each iteration:

$counter += N\ \&\ 1$

Here, $N\ \&\ 1$ checks the last bit of $N$.

If the last bit is $1$, then $counter$ increases by $1$.

If the last bit is $0$, then $counter$ does not increase.

After that:

$N=N>>1$

This shifts the bits of $N$ one position to the right.

So, the loop checks all bits of $N$ one by one.

Therefore, $counter$ stores the number of $1$ bits in the binary representation of $N$.

Finally, the function returns:

$counter==1$

This condition is true only when the binary representation of $N$ contains exactly one $1$.

A number which has exactly one $1$ in binary form is a power of $2$.

For example:

$2=(10)_2$

$4=(100)_2$

$8=(1000)_2$

So, the function returns $1$ if $N$ is a power of $2$, otherwise it returns $0$.

UGC NET Computer Science PYQ
Which of the following is correct way to declare a functional pointer in C ?





Go to Discussion

UGC NET Computer Science UGC NET PYQ UGC NET Computer Science UGC NET Computer Science August 2024 (Paper II) PYQ

Solution

Function pointer syntax:


return_type (*pointer_name)(arguments);


Example:


int (*func)(int,int);


Meaning:


func is a pointer


pointing to a function


returning int


taking two int parameters


Thus correct declaration:


int (*func)(int,int);


UGC NET Computer Science PYQ

Consider the following C code :

#include<stdio.h>


int temp = 0;


int fun(int x, int y)

{

    int z;

    temp++;


    if (y == 3)

        return (x*x*x);

    else

    {

        z = fun(x, y/3);

        return (z*z*z);

    }

}


int main()

{

    fun(4,81);

    printf("%d", temp);

}
What will be the output of the above code ?






Go to Discussion

UGC NET Computer Science UGC NET PYQ UGC NET Computer Science UGC NET Computer Science August 2024 (Paper II) PYQ

Solution

Recursive calls divide $y$ by $3$ each time.

Call sequence:

$fun(4,81)$
$fun(4,27)$
$fun(4,9)$
$fun(4,3)$

Total calls = 4

Each call increments temp.

So

$temp = 4$


UGC NET Computer Science PYQ

Which of the following are legal statements in C programming language?

(a) int *P = &44;
(b) int *P = &r;
(c) int P = &a;
(d) int P = a;






Go to Discussion

UGC NET Computer Science UGC NET PYQ UGC NET Computer Science UGC NET Computer Science Dec 2019 (Paper II) PYQ

Solution

Statement (a) is not legal because we cannot take the address of a constant value like $44$.

Statement (b) is legal because if $r$ is an integer variable, then &r gives its address and it can be stored in an integer pointer.

Statement (c) is not legal because &a gives address, but $P$ is declared as normal integer, not pointer.

Statement (d) is legal because if $a$ is an integer variable, then its value can be assigned to integer variable $P$.

So, legal statements are:

(b) and (d)


UGC NET Computer Science PYQ
int i, j;
for(i = 1; i < 5; i += 2)
    for(j = 1; j < i; j += 2)
        printf("%d", j);





Go to Discussion

UGC NET Computer Science UGC NET PYQ UGC NET Computer Science UGC NET Computer Science 26 June 2025 (Paper II) PYQ

Solution

For i = 1, inner loop condition j < i → 1 < 1 → false → no output.
For i = 3, inner loop runs with j = 1, prints 1, then j = 3 → condition fails.
Hence, final output is 1.

UGC NET Computer Science PYQ
Consider the operators used in C programming given below:
A. &&
B. +=
C. >>
D. >=
E. ?:
Choose among the following the correct order of precedence of the operators given above (higher to lower):





Go to Discussion

UGC NET Computer Science UGC NET PYQ UGC NET Computer Science UGC NET Computer Science 26 June 2025 (Paper II) PYQ

Solution

Operator precedence (highest → lowest) is: >> (shift) → >= (relational) → ?: (ternary) → += (assignment) → && (logical AND).

UGC NET Computer Science PYQ
#include<stdio.h>
void main()
{
    int arr[]={1, 2, 3, 4, 5};
    int *p = arr;

    printf("%d", *p++);
    printf("%d", *(p+1));
}
Find the output of the above code?





Go to Discussion

UGC NET Computer Science UGC NET PYQ UGC NET Computer Science UGC NET Computer Science August 2024 (Paper II) PYQ

Solution

Initial:


arr = {1,2,3,4,5}

p → arr[0]


First statement:


*p++


Post-increment


Value printed = *p = 1

Then pointer moves to next element.


Now:


p → arr[1]


Second statement:


*(p+1)


p+1 → arr[2]


Value = 3


Output:


1 3


UGC NET Computer Science PYQ

Consider the function in C code :

Cal(a, b)

{

    if (b != 1)

    {

        if (a != 1)

        {

            printf("*");

            Cal(a/2, b);

        }

    }

    else

    {

        b = b - 1;

        Cal(10, b);

    }

}

How many times * is going to be printed, if the function is called with


Cal(10, 10) ?






Go to Discussion

UGC NET Computer Science UGC NET PYQ UGC NET Computer Science UGC NET Computer Science August 2024 (Paper II) PYQ

Solution

Observe recursion:

Each call prints * while dividing a by 2.

Sequence:

10 → 5 → 2 → 1

For each b value, stars printed = 3.

b decreases from 10 to 1.

Thus:

$9 \times 3 = 27$

Total stars printed = 27.


UGC NET Computer Science PYQ

Consider the following code :

#include<stdio.h>


void f1(char *x, char *y) {

    char *t;

    t = x;

    x = y;

    y = t;

}


void f2(char **x, char **y) {

    char *t;

    t = *x;

    *x = *y;

    *y = t;

}


int main()

{

    char *a = "ONE", *b = "TWO";


    f1(a, b);

    printf("%s %s", a, b);


    f2(&a, &b);

    printf("%s %s", a, b);


    return 0;

}
What will be the output of the above code?






Go to Discussion

UGC NET Computer Science UGC NET PYQ UGC NET Computer Science UGC NET Computer Science August 2024 (Paper II) PYQ

Solution

Initial values

a = "ONE"
b = "TWO"

Step 1: f1(a, b)

Function parameters are passed by value.

Inside f1 the pointers swap locally:

x ↔ y

But original variables a and b do not change.

So after f1:

a = ONE
b = TWO

First printf prints:

ONE TWO

Step 2: f2(&a, &b)

Now addresses of pointers are passed.

Inside f2:

t = *x
*x = *y
*y = t

This swaps actual pointers a and b.

After swap:

a = TWO
b = ONE

Second printf prints:

TWO ONE

UGC NET Computer Science PYQ

Consider the following conditional code, which returns Boolean values

if $((x>25) \land (y>100))$
return $false$;

else if $((x \le 25) \land (y \le 100))$
return $true$;

else if $((x>25) \land (y \le 100))$
return $false$;

else
return $true$;

Simplify it by filling in the following blank with a single Boolean expression without changing the behaviour of the conditional code.

if $($ ________ $)$
return $true$;

else
return $false$;






Go to Discussion

UGC NET Computer Science UGC NET PYQ UGC NET Computer Science UGC NET Computer Science Dec 2022 Shift II (Paper II) PYQ

Solution

Given code returns $false$ when:

$((x>25) \land (y>100))$

or

$((x>25) \land (y \le 100))$

So,

$false = (x>25)$

Therefore, the code returns $true$ when:

$x \le 25$

Hence, the required Boolean expression is:

$x \le 25$


UGC NET Computer Science PYQ
void main() {
    int *i, a = 12, b = 2, c;
    c = (a = a + b, b = a / b, a = a * b, b = a - b);
    i = &c;
    printf("%d", -(*i));
}






Go to Discussion

UGC NET Computer Science UGC NET PYQ UGC NET Computer Science UGC NET Computer Science 26 June 2025 (Paper II) PYQ

Solution

Step-by-step explanation:
a = 12, b = 2
a = a + b → a = 14
b = a / b → b = 7
a = a * b → a = 98
b = a - b → b = 91
→ c = 91
Hence, -(*i) = -91
Output: 91 (negative of c printed as -(-91) → 91)

UGC NET Computer Science PYQ
Which of the following is wrong about the data types?





Go to Discussion

UGC NET Computer Science UGC NET PYQ UGC NET Computer Science UGC NET Computer Science Dec 2022 Shift II (Paper II) PYQ

Solution

For $n$ bits:

Unsigned range is:

$0$ to $2^n-1$

Signed range is:

$-2^{n-1}$ to $2^{n-1}-1$

So, signed data type does not have more range than unsigned data type.


UGC NET Computer Science PYQ

int x = 128, y = 110;

do {

    if (x > y) x = x - y;

    else       y = y - x;

} while (x != y);

printf("%d", x);






Go to Discussion

UGC NET Computer Science UGC NET PYQ UGC NET Computer Science UGC NET Computer Science 26 June 2025 (Paper II) PYQ

Solution

Repeated subtraction implements Euclid’s algorithm; the loop ends with x = y = gcd(128,110) = 2.

UGC NET Computer Science PYQ
Only legal pointer operations:
A. pointer + number → pointer
B. pointer – number → number
C. pointer + pointer → pointer
D. pointer – pointer → pointer
E. pointer – pointer → number
Choose the most appropriate answer from the options given below:





Go to Discussion

UGC NET Computer Science UGC NET PYQ UGC NET Computer Science UGC NET Computer Science 26 June 2025 (Paper II) PYQ

Solution

pointer + number → pointer (A)
pointer - pointer → number (E)

UGC NET Computer Science PYQ
What would be the equivalent pointer expression for referring the array element ar[m][n][o]?
  1. *(*(*(ar) + m + n) + o)
  2. (*(*(*(ar + m) + n) + o))
  3. (*(*(ar + m) + n) + o)
  4. *(*(*(ar + m) + n) + o)





Go to Discussion

UGC NET Computer Science UGC NET PYQ UGC NET Computer Science UGC NET Computer Science 26 June 2025 (Paper II) PYQ

Solution

Consider ar as a 3-dimensional array declared as:

data_type ar[a][b][c];

Here, the array element ar[m][n][o] can be represented in pointer form as:

*(*(*(ar + m) + n) + o)

Step-by-step understanding:

  • ar is a pointer to the first 2D array ar[0].
  • ar + m moves the pointer to the mth 2D array → ar[m].
  • *(ar + m) gives the address of the first 1D array inside ar[m].
  • *(ar + m) + n moves to the nth 1D array → ar[m][n].
  • *(*(ar + m) + n) gives the address of the first element of that 1D array.
  • *(*(*(ar + m) + n) + o) finally gives the element ar[m][n][o].

✅ Final Answer: *(*(*(ar + m) + n) + o)


UGC NET Computer Science PYQ
Consider the following pseudo-code fragment, where $a$ and $b$ are integer variables that have been initialized:

Pre-conditions: $(a>1\land a<b)$

Assume that overflow never occurs.

int $x=0$; int $p=1$;

while $(p<b)$ {
$p=p*a$;
$x=x+1$;
}

When the while loop terminates, what will be the value of $x$ in terms of $a$ and $b$?





Go to Discussion

UGC NET Computer Science UGC NET PYQ UGC NET Computer Science UGC NET Computer Science Nov 2020 (Paper II) PYQ

Solution

Initially,

$p=1=a^0$

After $1$ iteration:

$p=a^1$

After $2$ iterations:

$p=a^2$

After $x$ iterations:

$p=a^x$

Loop stops when:

$p\ge b$

So,

$a^x\ge b$

Minimum such value of $x$ is:

$x=\lceil \log_a b\rceil$

UGC NET Computer Science PYQ

Arrange the following in correct order, so that they can follow a proper run time environment :


A. Programming statement

B. Compilation

C. Memory allocation

D. Type of variable

E. Memory deallocation






Go to Discussion

UGC NET Computer Science UGC NET PYQ UGC NET Computer Science UGC NET Computer Science June 2024 (Paper II) PYQ

Solution

The correct execution flow in a programming environment is:


Programming statement (A) – Program is written.


Compilation (B) – Code is compiled to machine/intermediate code.


Type of variable (D) – Variables are recognized with their types.


Memory allocation (C) – Memory is allocated during execution.


Memory deallocation (E) – Memory is released after execution.


Thus the correct order is:


UGC NET Computer Science PYQ
What will be the output of the following code?
include <stdio.h>

int main() {

int a, b, c;

a = 0x10; b = 010;

c = a + b;

printf("%d", c);

return 0;

}





Go to Discussion

UGC NET Computer Science UGC NET PYQ UGC NET Computer Science UGC NET Computer Science June 2023 (Paper II) PYQ

Solution

0x10 is a hexadecimal number.

0x10 = 16

010 is an octal number.

010 = 8

So,

c = 16 + 8 = 24

UGC NET Computer Science PYQ
Suppose you are compiling on a machine with $1$-byte chars, $2$-byte shorts, $4$-byte ints, and $8$-byte doubles, and with alignment rules that require the address of every primitive data element to be an integer multiple of the element's size. Suppose further that the compiler is not permitted to reorder fields; padding is used to ensure alignment. How much space will be consumed by the following array?

struct {
short s;
char c;
short t;
char d;
double r;
int i;
} A[10];





Go to Discussion

UGC NET Computer Science UGC NET PYQ UGC NET Computer Science UGC NET Computer Science Nov 2020 (Paper II) PYQ

Solution

$short\ s$: starts at offset $0$, size $2$, next offset $2$

$char\ c$: starts at offset $2$, size $1$, next offset $3$

$short\ t$: needs multiple of $2$, so $1$ byte padding, starts at offset $4$, next offset $6$

$char\ d$: starts at offset $6$, size $1$, next offset $7$

$double\ r$: needs multiple of $8$, so $1$ byte padding, starts at offset $8$, next offset $16$

$int\ i$: needs multiple of $4$, starts at offset $16$, next offset $20$

Struct size must be multiple of maximum alignment $8$.

So, $20$ is padded to $24$ bytes.

For $A[10]$:

$10\times24=240$ bytes

UGC NET Computer Science PYQ
What is the output of following code?

main()

struct s1

{
char *z;

int i;

struct s1 *p;

}

static struct s1 a[] = {

{"Nagpur", 1, a + 1}

{"Raipur", 2, a + 2}

{"Kanpur", 3, a}

};

struct s1 *ptr = a;

printf("%s %s %s\n", a[0].z, ptr -> z, a[2].p -> z);

}





Go to Discussion

UGC NET Computer Science UGC NET PYQ UGC NET Computer Science UGC NET Computer Science June 2023 (Paper II) PYQ

Solution

a[0].z = Nagpur

ptr = a, so ptr -> z = a[0].z = Nagpur

a[2].p = a, so a[2].p -> z = a[0].z = Nagpur

Final Answer: (b) Nagpur Nagpur Nagpur

UGC NET Computer Science PYQ
Consider the following recursive Java function $f$ that takes two long arguments and returns a float value:

public static float f(long m, long n) {
float result = (float)m / (float)n;
if (m < 0 || n < 0) return 0.0f;
else result -= f(m * 2, n * 3);
return result;
}

Which of the following real values best approximates the value of $f(1,3)$?





Go to Discussion

UGC NET Computer Science UGC NET PYQ UGC NET Computer Science UGC NET Computer Science Nov 2020 (Paper II) PYQ

Solution


$f(1,3)=\frac{1}{3}-f(2,9)$

$=\frac{1}{3}-\frac{2}{9}+\frac{4}{27}-\frac{8}{81}+\cdots$

This is a geometric series with first term $\frac{1}{3}$ and common ratio $-\frac{2}{3}$.

Sum:

$\frac{\frac{1}{3}}{1-(-\frac{2}{3})}=\frac{\frac{1}{3}}{\frac{5}{3}}=\frac{1}{5}=0.2$

UGC NET Computer Science PYQ
How will you free the memory allocated by the following program?
#include <stdio.h>

#define MAXROW 3
#define MAXCOL 4

int main()
{
    int **p, i, j;

    p = (int **)malloc(MAXROW * sizeof(int*));

    return 0;
}






Go to Discussion

UGC NET Computer Science UGC NET PYQ UGC NET Computer Science UGC NET Computer Science June 2023 (Paper II) PYQ

Solution

Memory allocated using malloc() is released using free().


Here, memory is allocated to pointer p, so it should be freed as: free(p);


UGC NET Computer Science PYQ

In what sequence the initialization, testing and execution of body is done while using do-while loop?

I. Commenting
II. Execution of the body
III. Initialisation
IV. Testing the condition

Choose the correct answer from the following:






Go to Discussion

UGC NET Computer Science UGC NET PYQ UGC NET Computer Science UGC NET Computer Science Dec 2022 Shift II (Paper II) PYQ

Solution

In a do-while loop, initialization is done first.

Then the loop body executes at least once.

After executing the body, the condition is tested.

So, correct order is:

Initialisation → Execution of the body → Testing the condition

That is:

III, II, IV



UGC NET Computer Science


Online Test Series,
Information About Examination,
Syllabus, Notification
and More.

Click Here to
View More

UGC NET Computer Science


Online Test Series,
Information About Examination,
Syllabus, Notification
and More.

Click Here to
View More

Limited Seats
× Aspire MCA Promotion

Game Changer NIMCET Test Series 2026

Boost your preparation with mock tests, analysis and rank-focused practice.

JOIN NOW
Ask Your Question or Put Your Review.

loading...