Programming and Coding Questions with Answers

Basic Programming Concepts

  1. What is a variable in programming?
    A variable is a named storage location in memory that holds data that can be modified during program execution.
  2. What is a data type?
    A data type defines the type of data a variable can store, such as integers, floats, characters, and booleans.
  3. What is the difference between a strongly typed and a weakly typed language?
    In a strongly typed language such as Java or Python, data types must be explicitly declared and followed. In a weakly typed language such as JavaScript, types are more flexible.
  4. What is typecasting?
    Typecasting is converting a variable from one data type to another, such as from integer to float.
  5. What is the difference between implicit and explicit type conversion?
    Implicit conversion is done automatically by the compiler, whereas explicit conversion requires the programmer to specify the conversion.
  6. What is a constant in programming?
    A constant is a variable whose value cannot be changed during program execution.
  7. What is the difference between an identifier and a keyword?
    An identifier is the name of a variable, function, or class, while a keyword is a reserved word in the programming language.
  8. What is an operator in programming?
    An operator performs operations on variables and values, such as arithmetic operators like plus and minus, relational operators like greater than and less than, and logical operators like and and or.
  9. What is the difference between the equals sign and the double equals sign in programming?
    The single equals sign is an assignment operator, whereas the double equals sign is a comparison operator that checks equality.
  10. What is a ternary operator?
    The ternary operator is a shorthand for an if-else condition: condition question mark value if true colon value if false.

Control Flow & Looping

  1. What is a conditional statement?
    A conditional statement allows a program to make decisions using if, else, and switch statements.
  2. What is the difference between if-else and switch statements?
    If-else is used for conditional checks, while switch is used for selecting from multiple options based on a single value.
  3. What is a loop in programming?
    A loop allows a set of instructions to be repeated multiple times, such as for, while, and do-while loops.
  4. What is the difference between while and do-while loops?
    A while loop checks the condition before execution, whereas a do-while loop executes at least once before checking the condition.
  5. What is an infinite loop?
    An infinite loop runs endlessly because its termination condition is never met.
  6. What is a break statement?
    A break statement exits the loop immediately when a condition is met.
  7. What is a continue statement?
    A continue statement skips the current iteration of a loop and moves to the next iteration.
  8. What is a nested loop?
    A nested loop is a loop inside another loop, often used for matrix operations.
  9. What is the difference between recursion and iteration?
    Recursion uses function calls to solve problems, whereas iteration uses loops.
  10. What is tail recursion?
    Tail recursion is a type of recursion where the recursive call is the last operation in the function.

Functions & Modular Programming

  1. What is a function in programming?
    A function is a reusable block of code that performs a specific task.
  2. What are the advantages of using functions?
    Functions improve code reusability, modularity, and readability.
  3. What is the difference between a function definition and a function declaration?
    A function declaration specifies the function’s name and parameters, while a function definition includes the function’s implementation.
  4. What is the difference between a built-in and a user-defined function?
    A built-in function is provided by the language such as print in Python, while a user-defined function is created by the programmer.
  5. What is function overloading?
    Function overloading allows multiple functions with the same name but different parameters.
  6. What is function overriding?
    Function overriding occurs when a subclass provides a new implementation for an inherited function.
  7. What is a lambda function?
    A lambda function is an anonymous function that is defined in a single line.
  8. What is a callback function?
    A callback function is a function passed as an argument to another function to be executed later.
  9. What is the difference between pass by value and pass by reference?
    In pass by value, a copy of the argument is passed; in pass by reference, the actual variable is passed.
  10. What is the scope of a variable?
    The scope defines where a variable can be accessed, such as local, global, or block scope.

Object-Oriented Programming (OOPs)

  1. What is an object in OOP?
    An object is an instance of a class that contains properties and methods.
  2. What is encapsulation?
    Encapsulation is the practice of hiding data and providing access only through methods.
  3. What is inheritance?
    Inheritance allows a class to derive properties and methods from another class.
  4. What is polymorphism?
    Polymorphism allows a function or method to behave differently based on the object.
  5. What is abstraction in OOP?
    Abstraction hides implementation details and exposes only essential functionalities.
  6. What is a constructor in a class?
    A constructor is a special function used to initialize an object.
  7. What is a destructor in a class?
    A destructor is a function that is automatically called when an object is destroyed.
  8. What is method overloading?
    Method overloading allows multiple methods in a class with the same name but different parameters.
  9. What is method overriding?
    Method overriding allows a subclass to provide a specific implementation of a method already defined in its parent class.
  10. What is the difference between composition and inheritance?
    Composition involves including an object of one class inside another, while inheritance derives a new class from an existing one.