CLASS – X ICSE 2023-24
1. What does "Class as the Basis of all
Computation" refer to in the context of computer science?
"Class as the Basis of all
Computation" is a fundamental concept that highlights the significance of
classes in object-oriented programming, where classes are used to create
objects and define their behavior.
2. Define the term "object" in the
context of object-oriented programming.
An object is an instance of a class. It is a
self-contained unit that encapsulates data and behavior.
3. Explain the concept of "class" in
object-oriented programming.
A class is a blueprint or template for creating
objects. It defines the attributes (data) and methods (functions) that objects
of the class will have.
4. What is "inheritance" in
object-oriented programming, and why is it important?
Inheritance is a mechanism that allows a new
class to inherit properties and behaviors from an existing class. It promotes
code reuse and hierarchy in class structures.
5. How do you define a class in Python? Provide
an example.
A class in Python is defined using the class
keyword. Here's an example:
class Person:
def
__init__(self, name, age):
self.name = name
self.age = age
def
greet(self):
return f"Hello, my name is {self.name}, and I am {self.age} years
old."
6. Explain the purpose of the __init__ method
in Python classes.
The __init__ method is a constructor that
initializes object attributes when a new object is created from a class.
7. What is "encapsulation," and how
is it related to classes in programming?
Encapsulation is the bundling of data
(attributes) and methods (functions) that operate on the data into a single
unit called a class. It helps in data hiding and access control.
8. How can you create an object from a class in
Python?
You can create an object by calling the class
constructor. For example:
person1 = Person("Alice", 25)
9. What is the role of the "dot
notation" in accessing class attributes and methods?
The "dot notation" (e.g.,
object.attribute or object.method()) is used to access attributes and call
methods of an object.
10. Explain the term "polymorphism"
in object-oriented programming.
- Polymorphism allows objects of different
classes to be treated as objects of a common base class. It enables flexibility
and extensibility in code.
11. What is "abstraction," and why is
it important in class-based programming?
- Abstraction is the process of simplifying
complex systems by breaking them down into manageable parts. It helps in
modeling real-world entities using classes.
12. What is a "class variable" in
Python, and how does it differ from an instance variable?
- A class variable is shared among all
instances of a class, while an instance variable is specific to an individual
object of the class.
13. How does the concept of "composition"
relate to classes in object-oriented programming?
- Composition is a way to build complex classes
by combining or composing simpler classes as components. It enables the
creation of more sophisticated and modular systems.
14. Explain the term "constructor" in
the context of classes and objects.
- A constructor is a special method that
initializes the object when it is created. In Python, the constructor is named
__init__.
15. Describe the role of "getters"
and "setters" in class design.
- Getters are methods that retrieve the values
of object attributes, and setters are methods that modify those attribute
values while applying validation and logic.
16. What is "data hiding," and why is
it a fundamental principle in class-based programming?
- Data hiding restricts access to an object's
attributes and promotes the use of methods to interact with the data. It helps
maintain data integrity and security.
17. How does class-based programming promote
code reusability?
- Class-based programming allows you to create
reusable and modular code by defining classes and creating objects based on
those classes.
18. Explain the concept of "method
overloading" in object-oriented programming.
- Method overloading involves defining multiple
methods with the same name in a class but with different parameters. The
appropriate method is called based on the arguments provided.
19. What is the difference between a
"class method" and an "instance method" in Python classes?
- Class methods are called on the class itself
and have access to class-level attributes, while instance methods are called on
objects and have access to object-specific attributes.
20. How can you implement "class
inheritance" in Python, and what are the benefits?
- Class inheritance is implemented by creating
a new class that derives properties and behaviors from an existing class. It
promotes code reuse and hierarchy, allowing you to build more complex systems.
1. What is a user-defined method in
programming?
A user-defined method is a custom function
created by the programmer to perform a specific task or set of tasks in a
program.
2. How do you define a user-defined method in
Java?
A user-defined method is defined in Java using
the public or private access modifier, followed by the return type (e.g., void
for methods that don't return a value), the method name, and any parameters it
may take.
3. What is the purpose of a method signature in
a user-defined method?
The method signature includes the method name and
the parameter list (if any) and is used to uniquely identify the method within
the program.
4. How do you call a user-defined method in
Java?
To call a user-defined method, you use the
method name followed by parentheses, passing any required arguments inside the
parentheses. For example, methodName(argument1, argument2).
5. What is a return type, and why is it
important in user-defined methods?
The return type specifies the type of value
that a method returns or indicates that the method doesn't return a value (use
void). It's important for understanding what the method does and what it
provides as output.
6. Explain the difference between a method
parameter and an argument.
A method parameter is a variable declared in
the method's signature. An argument is the actual value passed to the method
when it is called.
7. How can you pass multiple arguments to a
user-defined method in Java?
Multiple arguments are separated by commas
within the method call, and the method should have corresponding parameters to
receive these arguments.
8. What is method overloading, and why is it
used in Java?
Method overloading is the ability to define
multiple methods with the same name but different parameter lists. It's used to
create methods that perform similar tasks with varying inputs.
9. How do you pass values to a user-defined
method and retrieve results from it?
Values are passed to a method as arguments, and
results are retrieved using the return statement. The method can return a value
that can be assigned to a variable.
10. What is the significance of modular
programming and user-defined methods in code development?
- Modular programming involves breaking down a
program into smaller, manageable, and reusable parts. User-defined methods are
essential for creating these modular components, enhancing code organization
and maintainability.
1. What is a constructor in object-oriented
programming?
A constructor is a special method used to
initialize objects of a class. It is automatically called when an object is
created.
2. What is the primary purpose of a constructor
in a class?
The primary purpose of a constructor is to
initialize the attributes or properties of an object when it is created.
3. How does a constructor differ from a regular
method in a class?
Constructors have the same name as the class
and are automatically invoked when an object is created. They don't have a
return type and can't be called explicitly like regular methods.
4. What is a default constructor, and when is
it invoked?
A default constructor is a constructor with no
parameters. It is automatically invoked when an object is created without
specifying any arguments.
5. What is a parameterized constructor, and
when is it used?
A parameterized constructor is a constructor
that accepts one or more arguments when an object is created. It is used to
initialize object attributes with specific values.
6. How can you create and use a constructor in
Java?
To create a constructor, define a method with
the same name as the class and initialize object attributes. Constructors are
called when objects are created using the new keyword.
7. Can a class have multiple constructors? If
so, why is this useful?
Yes, a class can have multiple constructors
with different parameter lists. This is useful for providing flexibility when
creating objects with various initialization options.
8. What is constructor overloading, and why is
it used?
Constructor overloading is the practice of
defining multiple constructors within a class with different parameter lists.
It allows objects to be created with different initializations based on the
provided arguments.
9. How does a constructor relate to the concept
of encapsulation in object-oriented programming?
Constructors are used to initialize object
attributes, and encapsulation involves bundling data (attributes) and methods
(functions) into a single unit (class). Constructors are responsible for initializing
and encapsulating data.
10. Explain the role of the 'this' keyword in
constructors.
- The 'this' keyword is used in constructors to
differentiate between instance variables and constructor parameters when they
share the same name. It specifies that you are referring to the instance
variable of the class.
1. What are library classes in the context of
programming?
Library classes are pre-written classes and
functions that are part of a programming language's standard library. They
provide a wide range of functionality to simplify programming tasks.
2. How do library classes enhance code
development in programming?
Library classes provide pre-implemented and
well-tested solutions for common programming tasks, which can save time and
effort for programmers.
3. What are some common examples of library
classes in Java?
Common examples of library classes in Java
include java.util.Scanner for input, java.util.ArrayList for dynamic arrays,
and java.io.File for file handling.
4. How do you import and use library classes in
your Java program?
You import library classes at the beginning of
your Java program using the import statement, and then you can use their
methods and classes in your code.
5. Explain the role of the java.util.Scanner
class in Java and its usage for input.
The java.util.Scanner class is used for input
in Java. It allows you to read data from various sources like the keyboard or
files. You create a Scanner object and use its methods to read input.
6. What is the purpose of the
java.util.ArrayList class, and how is it used?
The java.util.ArrayList class is a dynamic
array that can grow or shrink in size. It is used to store and manipulate a
collection of elements. You can add, remove, and access elements in an
ArrayList.
7. Describe the java.io.File class in Java and
its role in file handling.
The java.io.File class is used for file
handling in Java. It allows you to create, read, write, delete, and manage
files and directories on the file system.
8. What is the significance of the
java.lang.Math class in Java?
The java.lang.Math class provides a set of
mathematical functions and constants, making it easier to perform common
mathematical operations in Java programs.
9. Explain the role of the java.util.Random
class in generating random numbers.
The java.util.Random class is used to generate
pseudo-random numbers. It provides methods to generate random integers,
doubles, and other data types.
10. How can you find and use library classes in
Python, and what is an example of a Python library class?
- In Python, library classes are part of the
standard library and can be imported and used directly. An example is the math
library, which provides mathematical functions like sqrt() for square root
calculation and pi for the mathematical constant π.
1. What is encapsulation in the context of
object-oriented programming?
Encapsulation is one of the four fundamental OOP
concepts and refers to the bundling of data (attributes) and methods
(functions) that operate on that data into a single unit called a class.
2. Why is encapsulation important in
object-oriented programming?
Encapsulation promotes data hiding and access
control, enhancing code organization, security, and maintainability.
3. What is a class in object-oriented
programming, and how does it relate to encapsulation?
A class is a blueprint for creating objects. It
defines both the attributes (data) and methods (functions) that encapsulate an
object's behavior and data.
4. Explain the concept of access modifiers
(private, protected, public) and their role in encapsulation.
Access modifiers control the visibility and
accessibility of class members (attributes and methods). Private members are
only accessible within the class, protected members are accessible within the
class and its subclasses, and public members are accessible from anywhere.
5. How do you designate a class attribute or
method as "private" in Java?
In Java, you use the private access modifier to
declare a class attribute or method as private. For example: private int
myVariable;
6. Describe a scenario where encapsulation can
protect sensitive data in an object.
Encapsulation can protect sensitive data, such
as a password, by making it private. Access to the data is controlled through
public methods that validate and manage access.
7. What is the benefit of encapsulating data
within a class using getter and setter methods?
Getter methods allow controlled access to
private data, and setter methods enable validation and manipulation of data,
maintaining data integrity.
8. How do you create getter and setter methods
in Java for private class attributes?
Getter methods retrieve the value of a private
attribute, and setter methods set its value. For example:
public int getMyVariable() {
return myVariable;
}
public void setMyVariable(int value) {
if
(value >= 0) {
myVariable = value;
}
}
9. What is data hiding, and how does it relate
to encapsulation?
Data hiding is the practice of restricting
direct access to a class's attributes and methods. Encapsulation enforces data
hiding by making data private and controlling access through methods.
10. Explain how encapsulation enhances code
security and maintainability.
- Encapsulation improves code security by
limiting unauthorized access to data and ensures that data is manipulated only
through validated methods. It enhances maintainability by isolating data and
behavior within a class, making it easier to update and modify code without
affecting other parts of the program.
1. What is an array in programming?
An array is a data structure that allows you to
store a collection of elements of the same data type under a single name.
2. How are array elements accessed in most
programming languages?
Array elements are accessed using an index,
which is a numerical value that specifies the position of the element within
the array.
3. What is the index of the first element in an
array?
In most programming languages, the index of the
first element in an array is 0.
4. What is the length or size of an array?
The length or size of an array is the total
number of elements it can hold. It is determined when the array is declared.
5. How do you declare and initialize an array
in Java?
In Java, you can declare and initialize an
array as follows:
int[] numbers = new int[5]; // Declares an
integer array with a size of 5
6. What is a one-dimensional array, and how is
it different from a two-dimensional array?
A one-dimensional array is a simple array with
a single row of elements. A two-dimensional array is an array of arrays,
forming a grid with rows and columns.
7. How do you access elements of a
two-dimensional array in most programming languages?
In a two-dimensional array, you access elements
using two indices: one for the row and one for the column.
8. What is an array index out of bounds error,
and how can it be avoided?
An array index out of bounds error occurs when
you try to access an element with an index that is not within the valid range
of the array. To avoid this error, always ensure that the index is within the
bounds of the array.
9. Explain the concept of dynamic arrays or
ArrayLists.
Dynamic arrays, such as ArrayLists in Java, can
grow or shrink in size as needed. They provide a more flexible alternative to
static arrays.
10. What is the difference between an array and
an ArrayList in Java?
- An array has a fixed size, while an ArrayList
can dynamically resize. To use an ArrayList, you need to import the
java.util.ArrayList class, while arrays are part of the core language.
1. What is a string in programming?
A string is a sequence of characters, such as
letters, numbers, and symbols, that is used to represent text.
2. How do you declare and initialize a string
in Java?
In Java, you can declare and initialize a
string using double quotes, like this:
String myString = "Hello, World!";
3. Explain the concept of string concatenation.
String concatenation is the process of
combining two or more strings to create a new, longer string. In Java, you can
use the + operator for string concatenation.
4. What is the length of a string, and how can
you find it in Java?
The length of a string is the number of
characters it contains. In Java, you can use the length() method to find the
length of a string.
5. How do you compare two strings in Java to
check if they are equal?
In Java, you can use the equals() method to
compare two strings for equality. For example:
String str1 = "Hello";
String str2 = "World";
boolean areEqual = str1.equals(str2); // This
will be false
6. What is the purpose of the charAt() method
in string handling?
The charAt() method in Java allows you to
retrieve the character at a specific index within a string.
7. How can you convert a string to uppercase or
lowercase in Java?
In Java, you can use the toUpperCase() and
toLowerCase() methods to convert a string to uppercase or lowercase,
respectively.
8. What is the difference between the
substring() and split() methods in Java string handling?
The substring() method extracts a part of a
string based on indices, while the split() method splits a string into an array
of substrings based on a delimiter.
9. Explain the concept of string immutability
in Java.
In Java, strings are immutable, meaning they
cannot be modified after creation. Any operation that appears to modify a
string actually creates a new string.
10. How can you remove leading and trailing
spaces from a string in Java?
- You can use the trim() method to remove
leading and trailing spaces from a string in Java.