A variable is a placeholder used to store data in memory. It is an identifier for a specific value that can change during program execution. It is essential to define the data type of a variable in many programming languages, like integers or strings.
Which of the following is a correct way to declare a variable in Python?
A declare x = 10
B x = 10
C int x = 10
D int x:10
In Python, you do not need to explicitly declare the data type of a variable. You simply assign a value to a variable using the syntax variable_name = value. Python automatically infers the data type based on the value assigned.
What is the purpose of a loop in programming?
A To create functions
B To skip sections of code
C To repeat code
D To execute code once
A loop allows you to execute a block of code multiple times as long as a specified condition is true. It helps avoid repetitive code and makes programs more efficient by automating repetitive tasks.
Which operator is used for equality comparison in C?
A !=
B ==
C =
D ===
In C, the == operator is used to check if two values are equal. The = operator is used for assignment, not comparison. The === operator is not valid in C but is used in JavaScript for strict equality.
In Python, how do you define a function?
A def function_name()
B create function_name()
C function = def()
D function function_name()
In Python, functions are defined using the def keyword followed by the function name and parentheses. This structure allows you to define reusable blocks of code that can be called throughout your program.
What does an exception in programming represent?
A A variable type
B A program error
C A data structure
D A type of loop
An exception is an event that disrupts the normal flow of execution in a program, typically caused by errors like dividing by zero, accessing invalid memory, or file not found. It is handled using exception handling techniques.
What is the purpose of a constructor in Object-Oriented Programming (OOP)?
A To create methods
B To define a class
C To declare variables
D To initialize an object
In OOP, a constructor is a special method used to initialize objects of a class. It is called automatically when an object is created, allowing you to set initial values for the object’s attributes.
Which of the following is a valid identifier in Java?
A variable_1
B variable#
C 2variable
D @variable
In Java, a valid identifier can start with a letter, underscore, or dollar sign and can include numbers after the first character. Identifiers cannot begin with a number or contain special characters like @ or #.
Which type of memory allocation does malloc perform in C?
A Heap allocation
B Dynamic allocation
C Static allocation
D Stack allocation
The malloc function in C is used to allocate memory dynamically at runtime. This memory is usually allocated on the heap and is not automatically freed, requiring manual deallocation using free().
What does the return statement do in a function?
A Returns the function name
B Ends the program
C Starts the function
D Sends a value from the function
The return statement in programming sends a value from the function back to the caller. It effectively ends the functionโs execution and passes the result to the point where the function was called.
What is the output of print(10 // 3) in Python?
A 0
B 3
C 10
D 3.33
In Python, the // operator performs integer division, which returns the quotient without the remainder. Therefore, 10 // 3 equals 3 as it discards the decimal part.
Which of these data types is used to store decimal numbers in C?
A bool
B int
C char
D float
In C, the float data type is used to store numbers with decimal points. The int type is for integers, while char is for storing single characters, and bool is for boolean values (true/false).
What does sizeof() do in C?
A Returns the data type
B Returns the memory address
C Returns the size of a data type
D Returns the value of a variable
The sizeof() operator in C returns the size (in bytes) of a variable or data type. It helps determine the amount of memory required for a variable or an array.
Which of the following is NOT a feature of Object-Oriented Programming?
A Encapsulation
B Compilation
C Inheritance
D Polymorphism
Compilation is a process related to translating source code into executable code, not a feature of OOP. OOP focuses on the four main principles: encapsulation, inheritance, polymorphism, and abstraction.
Which data structure is used to store key-value pairs?
A Dictionary
B Queue
C Array
D List
A dictionary (also called a map or hash map in other languages) stores data in key-value pairs. It allows fast lookups using keys to access corresponding values, making it ideal for associative arrays.