Cognizant interview prep
11 practice questions on the topics candidates reported from Cognizant interviews, with answers. The questions are ours; each one links to the interview report that names its topic.
Java 1
-
In public static void main(String[] args), why is main static?
- So it runs faster than other methods
- So it cannot be overridden by a subclass
- So the JVM can call it without first creating an object of the class
- So args can be changed by the operating system
Show the answer
C. So the JVM can call it without first creating an object of the class
The JVM starts the program by calling main on the class itself; there is no object yet. public lets the JVM reach it, void means it returns nothing, and args holds the command-line arguments.
This topic is named in a candidate’s interview report: GenC interview, 2022 batch, on GeeksforGeeks.
Python 5
-
What does this print?
a = [4, 7, 1, 9, 3, 2] even = sum(a[0::2]) odd = sum(a[1::2]) print(even - odd)- -10
- 10
- 8
- -18
Show the answer
A. -10
a[0::2] takes indices 0, 2 and 4: 4 + 1 + 3 = 8. a[1::2] takes indices 1, 3 and 5: 7 + 9 + 2 = 18. 8 - 18 = -10.
This topic is named in a candidate’s interview report: GenC Elevate interview, 2021, on GeeksforGeeks.
-
What does this print?
from collections import Counter c = Counter("programming") print(sorted(ch for ch, n in c.items() if n > 1))- ['r', 'g', 'm']
- ['m']
- ['g', 'm', 'r']
- ['g', 'm', 'n', 'r']
Show the answer
C. ['g', 'm', 'r']
Counter counts each character: r, g and m appear twice, every other letter once. sorted puts the repeated ones in alphabetical order.
This topic is named in a candidate’s interview report: GenC Elevate interview, 2021, on GeeksforGeeks.
-
What does this print?
a, b = 0, 1 while a <= 20: print(a, end=" ") a, b = b, a + b- 0 1 1 2 3 5 8 13
- 0 1 1 2 3 5 8 13 21
- 1 1 2 3 5 8 13
- 0 1 2 3 5 8 13
Show the answer
A. 0 1 1 2 3 5 8 13
The loop prints a while it is at most 20. The terms are 0, 1, 1, 2, 3, 5, 8, 13; the next term, 21, fails the test and is not printed.
This topic is named in a candidate’s interview report: GenC interview, 2022, on GeeksforGeeks.
-
Which is true of a Python list and a Python dict?
- Both keep their items sorted
- A dict can hold the same key twice
- Finding a value in a list with the in operator takes O(1) time
- A list is indexed by position; a dict maps unique keys to values and finds a key in O(1) time on average
Show the answer
D. A list is indexed by position; a dict maps unique keys to values and finds a key in O(1) time on average
A list is an ordered sequence you index by position, and searching it by value is O(n). A dict is a hash table of unique keys, so lookup by key is O(1) on average.
This topic is named in a candidate’s interview report: GenC technical interview, 2022 batch, on GeeksforGeeks.
-
In Python, which code fetches a web page's HTML as text?
- open("https://example.com").read()
- import https; https.page("example.com")
- print("https://example.com")
- requests.get("https://example.com", timeout=10).text
Show the answer
D. requests.get("https://example.com", timeout=10).text
requests (or urllib.request in the standard library) sends the HTTP request. Check resp.status_code or call resp.raise_for_status() before using the text, and always set a timeout.
This topic is named in a candidate’s interview report: GenC Next interview, 2025, on Reddit.
C and C++ 1
-
What does marking a small C++ function inline do?
- Lets the compiler replace calls with the function's body to save call overhead; it is a request the compiler may ignore
- Runs the function on a separate thread
- Makes the function private to its source file
- Prevents the function from ever being called through a pointer
Show the answer
A. Lets the compiler replace calls with the function's body to save call overhead; it is a request the compiler may ignore
inline suggests substituting the body at the call site. Modern compilers decide for themselves; the keyword's firm effect today is allowing the same definition to appear in several files, as in a header.
This topic is named in a candidate’s interview report: GenC technical interview, 2022 batch, on GeeksforGeeks.
DBMS and SQL 3
-
Which two rules does a primary key enforce?
- Unique and not null
- Unique and indexed only
- Not null and sorted
- Foreign and unique
Show the answer
A. Unique and not null
Every row needs a primary key value and no two rows may share one. Most databases also build an index on it, but that is how it is enforced, not what it means.
This topic is named in a candidate’s interview report: GenC interview, 2022 batch, on GeeksforGeeks.
-
Which statement creates a student table whose roll_no column is the primary key?
- CREATE student TABLE (roll_no INT KEY, name VARCHAR(50));
- MAKE TABLE student (roll_no INT PRIMARY, name VARCHAR(50));
- CREATE TABLE student (roll_no PRIMARY KEY INT, name VARCHAR(50));
- CREATE TABLE student (roll_no INT PRIMARY KEY, name VARCHAR(50));
Show the answer
D. CREATE TABLE student (roll_no INT PRIMARY KEY, name VARCHAR(50));
CREATE TABLE name (column type constraints, ...). PRIMARY KEY comes after the column's type, and makes roll_no unique and not null.
This topic is named in a candidate’s interview report: GenC interview, 2022 batch, on GeeksforGeeks.
-
users(id, email) has duplicate emails. Which statement keeps only the row with the smallest id for each email?
- DELETE FROM users WHERE id NOT IN (SELECT MIN(id) FROM users GROUP BY email);
- DELETE FROM users WHERE email IS DUPLICATE;
- DELETE DISTINCT FROM users;
- DELETE FROM users GROUP BY email;
Show the answer
A. DELETE FROM users WHERE id NOT IN (SELECT MIN(id) FROM users GROUP BY email);
The subquery picks one id to keep per email; everything else is deleted. MySQL needs the subquery wrapped once more (SELECT * FROM (...) t), because it cannot read the table it is deleting from directly.
This topic is named in a candidate’s interview report: GenC Next interview, 2025, on Reddit.
Operating systems 1
-
Which pair is an example of primary memory and secondary memory, in that order?
- RAM and an SSD
- An SSD and RAM
- CPU cache and registers
- A hard disk and a USB drive
Show the answer
A. RAM and an SSD
Primary memory (RAM, cache) is accessed directly by the CPU and is volatile. Secondary memory (SSD, hard disk) keeps data without power but must be loaded into RAM before the CPU can use it.
This topic is named in a candidate’s interview report: GenC interview, 2022 batch, on GeeksforGeeks.