Font size
WorksheetsTCS NQT Coding Test-4
Total questions: 75
Worksheet time: 2hrs 43mins
What is the result of following Program in python3
a=4
b=2
c=a-b*6
print("\n a={0} \n b={1} \n c={2}".format(a,b,c))
12
-8
8
-12
What is the result of following Program in python3
for i in range(3,0,-1)
print("{0} Python".format(i))
3 Python
2 Python
1 Python
2 Python
1 Python
0 Python
3 Python
error
What is the result of following Program in python3
if(32%2==0):
print("32")
else:
print("2")
2
32
error
What is the result of following Program in python3
for i in range(3,1):
print(i)
3
2
3
2
1
error
2
1
0
What is the result of following Program in python3
for i in range(1,4):
print(i)
if(i==2):
break;
1
1
2
1
2
3
4
error
What is the result of following Program in python3
for i in range(1,4):
print(i)
if(i==2):
continue;
1
1
2
3
error
1
3
person = input('Enter your name: ')
print('Hello', + person)
x = int(input("Please enter an integer: "))
if x<0:
x=0
print("Your negative number was changed to zero")
hello world
document.write('hello world');
using System;
class Program
{
public static void Main(string[] args)
{
Console.WriteLine("Hello, world!");
}
}
If a=(1,2,3,4), a[1:-1] is _________
a) Error, tuple slicing doesn’t exist
b) [2,3]
c) (2,3,4)
d) (2,3)
Suppose list1 is [2, 33, 222, 14, 25], What is list1[:-1]?
a) [2, 33, 222, 14]
b) Error
c) 25
d) [25, 14, 222, 33, 2]
What is the order of precedence in python?
a) Exponential, Parentheses, Multiplication, Division, Addition, Subtraction
b) Exponential, Parentheses, Division, Multiplication, Addition, Subtraction
c) Parentheses, Exponential, Multiplication, Division, Subtraction, Addition
d) Parentheses, Exponential, Multiplication, Division, Addition, Subtraction
Which of the following functions is a built-in function in python?
a) factorial()
b) print()
c) seed()
d) sqrt()
To add a new element to a list we use which Python command?
a) list1.addEnd(5)
b) list1.addLast(5)
c) list1.append(5)
d) list1.add(5)
What will be the output of the following Python statement?
>>>"abcd"[2:]
a) a
b) ab
c) cd
d) dc
What will be the output of the following Python code?
>>> str1 = 'hello'
>>> str2 = ','
>>> str3 = 'world'
>>> str1[-1:]
a) olleh
b) hello
c) h
d) o
What will be the output of the following Python code?
>>>str1="helloworld"
>>>str1[::-1]
a) dlrowolleh
b) hello
c) world
d) helloworld
Suppose list1 is [2445,133,12454,123], what is max(list1)?
a) 2445
b) 133
c) 12454
d) 123
Which of the following statements create a dictionary?
a) d = {}
b) d = {“john”:40, “peter”:45}
c) d = {40:”john”, 45:”peter”}
d) All of the mentioned
What will be the output of the following Python code snippet?
d = {"john":40, "peter":45}
d["john"]
a) 40
b) 45
c) “john”
d) “peter”
What will be the output of the following Python code?
a={1:"A",2:"B",3:"C"}
b={4:"D",5:"E"}
a.update(b)
print(a)
a) {1: ‘A’, 2: ‘B’, 3: ‘C’}
b) Method update() doesn’t exist for dictionaries
c) {1: ‘A’, 2: ‘B’, 3: ‘C’, 4: ‘D’, 5: ‘E’}
d) {4: ‘D’, 5: ‘E’}
Find the output of the following:-
int sum=0, n=10;
for( int i=n; i>=1; -- i)
{ sum= sum+ i; }
System.out.println(" Sum is "+ sum) ;
55
10
28
50
Consider these functions:
push() : push an element into the stack
pop() : pop the top-of-the-stack element
top() : returns the item stored in top-of-the-stack-node
What will be the output after performing these sequence of operations
push(20);
push(4);
top();
pop();
pop();
pop();
push(5);
top();
20
4
5
Stack Underflow
what is the ouput of the below program?
int main()
{
int i=10, j=20;
if((i=100) && (j==20))
{
printf("Have a nice day");
}
else
{
printf("condition not true");
}
return 0;
}
No output
Have a nice day
condition not true
Complier Error
What is the output here:
#include <stdio.h>
int main()
{
int i = 1, 2, 3;
printf("%d", i);
return 0;
}
1
2
3
Compilation Error
What is the output of the below program?
#include <stdio.h>
int main()
{
int i = 0;
switch (i)
{
case '0': printf("Enrich");
break;
case '1': printf("Grow");
break;
default: printf("EnrichnGrow");
}
return 0;
}
Enrich
Grow
EnrichnGrow
Compile Time error
what is the output?
#include<stdio.h>
int main()
{
int n;
for (n = 9; n!=0; n--)
printf("n = %d", n--);
return 0;
}
n=9 n=7 n= 5 n=3 n=1
n=9 n=8 n=7 n=6 n=5 n=4 n=3 n=2 n=1
Infinite loop
n=9 n=7 n= 5 n=3
How many times do while loop is guaranteed to execute?
0
1
depends upon condition
No gurantee
What is the final value of x when following code is run?
int x;
For(x=0;x<10;x++)
{
Printf(“%d”,x);
}
9
10
0
8
What is the output ?
int var;
var = !(0 && (2||1));
printf("%d",var);
0
2
1
Compiler Error
what is the value of j displayed ?
#include <stdio.h>
int i=9;
int j=0;
int main()
{
int i=10;
i++;
j=i;
printf("value of j is %d", j);
return 0;
}
9
10
11
0
What is the output?
#include <stdio.h>
int main()
{
int i;
for (i = 1; i != 10; i += 2)
printf(" QuizC ");
return 0;
}
QuizC QuizC QuizC QuizC QuizC
QuizC QuizC QuizC …. infinite times
QuizC QuizC QuizC QuizC
QuizC QuizC QuizC QuizC QuizC QuizC
The value of j at the end of the execution of the following C program:
int incr(int i)
{
static int count=0;
count=count+i;
return(count);
}
main()
{
int i,j;
for(i=0;i<=4;i++)
j=incr(i);
}
10
4
6
7
Consider the following C declaration :
struct {
short s[5]
union{
float y;
long z;
}u;
}t;
Assuming 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 alignmenmt considerations, is :
22 bytes
18 bytes
14 bytes
10 bytes
What will be the output of the following C program segment ?
char inchar='A';
switch(inchar)
{
case\\\'A\\\'n:
printf("choice A\\\n");
case\\\'B\\\';
printf("choice B");
case\\\'C\\\';
case\\\'D\\\';
case\\\'E\\\';
Default :
printf("No Choice");
}
No Choice
Choice A
Choice A Choice B No Choice
Program gives no output as it is erroneous
Consider the following C program
main()
{
int x,y,m,n;
scanf("%d%d",&x,&y);
/* x>0 and y>0 */
m=x;n=y;
while(m!=n)
{
if(m>n)
m=m-n;
else
n=n-m;
}
printf("%d",n);
}
x+y using repeated subtraction
x mod y using repeated subtraction
the greatest common divisior of x and y
the least common multiple of x and y
Consider the following C function :
int f(int n)
{
static int i=1;
if(n>=5)
return n;
n=n+i;
i++;
return f(n);
}
The value return by f(1) is
5
6
7
8
Consider the following C function :
void swap(int a,int b)
{
int temp;
temp=a;
a=b;
b=temp;
}
In order to exchange the value of two variable x and y:
call swap(x,y)
call swap(&x,&y)
swap(x,y) cannot be used as it does not return any value
swap(x,y) cannot be used as the parameters are passed by value
What does the following fragment of C program print?
char c[]="GATE2011";
char *p=c;
printf("%s",p+p[3]-p[1];
GATE2011
E2011
2011
011
Consider the following C program segment:
char p[20];
char*s="string";
int length=strlen(s);
int i;
for(i=0;i<length;i++)
p[i]=s[length-i];
printf("%s",p);
The output of the program is :
gnirts
gnirt
string
No output
Consider the following recursive C function that takes two arguments
unsigned int foo(unsigned int n,unsigned int r)
{if(n>0) return (n%r+foo(n/r,r));
else return 0;
What is the value of foo(513,2)
9
8
5
2
What is the range of values that can be stored by int datatype in C?
-(2^31) to (2^31) - 1
-256 to 255
-(2^63) to (2^63) - 1
0 to (2^31) - 1
What will be the output of the following code snippet?
#include <stdio.h>
int main() {
int a = 3, b = 5;
int t = a;
a = b;
b = t;
printf("%d %d", a, b);
return 0;
}
3 5
3 3
5 5
5 3
How is an array initialized in C language?
int a[3] = {1, 2, 3};
int a = {1, 2, 3};
int a[] = new int[3]
int a(3) = [1, 2, 3];
What is the output of the following code snippet?
#include <stdio.h>
int main() {
int a[] = {1, 2, 3, 4};
int sum = 0;
for(int i = 0; i < 4; i++) {
sum += a[i];
}
printf("%d", sum);
return 0;
}
1
4
20
10
What is the output of the following code snippet?
int main() {
int sum = 2 + 4 / 2 + 6 * 2;
printf("%d", sum);
return 0;
}
2
15
16
18
How is the 3rd element in an array accessed based on pointer notation?
*a + 3
*(a + 3)
(a + 3)
&(a + 3)
How are String represented in memory in C?
An array of characters.
The object of some class.
Same as other primitive data types.
LinkedList of characters.
What will be the output of the following code snippet?
#include <stdio.h>
void solve() {
int a[] = {1, 2, 3, 4, 5};
int sum = 0;
for(int i = 0; i < 5; i++) {
if(i % 2 == 0) {
sum += *(a + i);
}
else {
sum -= *(a + i);
}
}
printf("%d", sum);
}
int main() {
solve();
return 0;
}
2
15
Syntax Error
3
What will be the output of the following code snippet?
#include <stdio.h>
void solve() {
int first = 10, second = 20;
int third = first + second;
{
int third = second - first;
printf("%d ", third);
}
printf("%d", third);
}
int main() {
solve();
return 0;
}
10 30
30 10
10 20
20 10
What is the disadvantage of arrays in C?
The amount of memory to be allocated should be known beforehand.
Elements of an array can be accessed in constant time.
Elements are stored in contiguous memory blocks.
Multiple other data structures can be implemented using arrays.
What will be the output of the following code snippet?
#include <stdio.h>
void solve() {
int a = 3;
int res = a++ + ++a + a++ + ++a;
printf("%d", res);
}
int main() {
solve();
return 0;
}
12
24
20
18
What will be the value of x in the following code snippet?
#include <stdio.h>
void solve() {
int x = printf("Hello");
printf(" %d", x);
}
int main() {
solve();
return 0;
}
10
5
1
0
What does the following declaration indicate?
int x: 8;
x stores a value of 8.
x is an 8-bit integer.
Both A and B.
None of the above.
Which of the following is the proper syntax for declaring macros in C?
#define long long ll
#define ll long long
#define ll
#define long long
Which of the following is an exit controlled loop?
While loop.
For loop.
do-while loop.
None of the above.
What is the size of the int data type (in bytes) in C?
4
8
2
1
What will be the output of the following code snippet?
#include <stdio.h>
void swap(int a, int b) {
int t = *a;
a = b;
*b = t;
}
void solve() {
int a = 3, b = 5;
swap(&a, &b);
printf("%d %d", a, b);
}
int main() {
solve();
return 0;
}
3 5
5 3
5 5
3 3
How to declare a double-pointer in C?
int *val
int **val
int &val
int *&val
If p is an integer pointer with a value 1000, then what will the value of p + 5 be?
1020
1005
1004
1010
What will be the output of the following code snippet?
#include <stdio.h>
#define VAL 3 * (2 + 6)
void solve() {
int a = 10 + VAL;
printf("%d", a);
}
int main() {
solve();
return 0;
}
104
24
10
34
Which of the following are not standard header files in C?
stdio.h
stdlib.h
conio.h
None of the above.
What will be the output of the following code snippet?
#include <stdio.h>
void solve() {
printf("%d %d", (023), (23));
}
int main() {
solve();
return 0;
}
023 23
23 23
19 23
23 19
What will be the output of the following code snippet?
#include <stdio.h>
void solve() {
printf("%d %d %d", (076), (28), (0x87));
}
int main() {
solve();
return 0;
}
76 28 87
076 28 0x87
62 28 135
0 0 0
What will be the result of the following code snippet?
#include <stdio.h>
void solve() {
char ch[10] = "abcdefghij";
int ans = 0;
for(int i = 0; i < 10; i++) {
ans += (ch[i] - 'a');
}
printf("%d", ans);
}
int main() {
solve();
return 0;
}
45
36
20
100
What will be the output of the following code snippet?
#include <stdio.h>
void solve() {
bool ok = false;
printf(ok ? "YES" : "NO");
}
int main() {
solve();
return 0;
}
Yes
No
Compilation Error
None of the above
Full form of API
Application Process Interchange
Application Programs Interchange
Application Process Interface
Application Programming Interface
Connector Library to connect MySQL with Python IDLE is
pymysql
mysql.connector
both a and b
none of these
import mysql.connector as mys
mydb = mys.connect( host="localhost", user="root",
password="root"
)
mycursor = mydb.cursor()
mycursor.execute("CREATE DATABASE KVSEC3")
AFTER EXECUTING THE ABOVE CODE. What will be done
(a)
To display all the databases of MySQL with the help of cursor mycursor , command will be
mycursor.execute("SHOW DATABASES")
mycursor.execute("DESC DATABASES")
mycursor.executes("SHOW DATABASES")
mycursor.execute("SHOW ALL DATABASES")
Correct the error if any
mycursor = mydb.cursor()
mycursor.execute("CREATE TABLE customers (name VARCHAR(25), 'address' VARCHAR(55))")
mycursor.execute('CREATE TABLE customers (name VARCHAR(25), 'address' VARCHAR(55))')
mycursor.execute("CREATE TABLE customers (name VARCHAR(25), address VARCHAR(55))')
mycursor.execute("CREATE TABLE customers (name VARCHAR(25), address VARCHAR(55));")
mycursor.execute("CREATE TABLE customers (name VARCHAR(25), address VARCHAR55))")
Which of these is the best description of a list in Python?
A list is a collection of data that has an order and can be changed
A list is a lot of variables
A list is used for shopping
A list is a collection of data that cannot hold duplicated data and cannot be changed
