Chapter-1 Programming Fundamentals


1. What is a Class in Java?

A class in Java is a blueprint or template for creating objects.
It defines the properties (fields/variables) and behaviors (methods) that objects of that class will have.
Example of a Class in Java:
class Car {
  String brand; // Property
  int speed; // Property
  void accelerate() { // Method
    speed += 10;
    System.out.println("Speed increased to: " + speed);
  }
}

2. What is an Object in Java?

An object in Java is an instance of a class.
It represents a real-world entity with state (attributes/fields) and behavior (methods). Objects are created using the new keyword.
Example of an Object in Java:
class Car {
  String brand; // Property
  int speed; // Property
  void accelerate() { // Method
    speed += 10;
    System.out.println("Speed increased to: " + speed);
  }
}
public class Main {
  public static void main(String[] args) {
    Car myCar = new Car(); // Creating an object
    myCar.brand = "Toyota";
    myCar.speed = 50;
    myCar.accelerate(); // Calling method
  }
}

3. What is Data Member (Field/Attribute)?

A data member in Java is a variable that belongs to a class or an object.
It stores the state (properties) of an object.
Example of a Data Member:
class Car {
  String brand; // Data member (instance variable)
  int speed; // Data member
}

4. What is Method?

A method in Java is a block of code that performs a specific task.
Methods define the behavior of an object.
Example of a Method:
class Car {
  String brand;
  int speed;
  void accelerate() { // Method
    speed += 10;
    System.out.println("Speed increased to: " + speed);
  }
}

5. What are access specifiers in Java?

Access specifiers in Java define the scope and visibility of classes, methods, and variables.
They control whether other classes can access specific members in the same class, package, or different packages.

6. How many types of access specifiers are in Java?

Java has four access specifiers: public, private, protected, and default (no modifier).
These define different levels of accessibility for class members within the same class, package, or other packages.

7. What is the default access specifier in Java?

If no access specifier is used, it is considered "default." A default member is accessible only within the same package and cannot be accessed from outside the package, even if inherited.

8. What is the scope of the public access specifier?

A public member can be accessed from anywhere in the program, including other classes and packages. It has the widest scope among all access specifiers and is commonly used for APIs and utility classes.

9. Can a private member be accessed outside its class?

No, private members can only be accessed within the same class. They are not visible to subclasses or any other class, ensuring encapsulation and preventing direct modification from outside the class.

10. What is the difference between protected and default access specifiers?

A protected member is accessible within the same package and by subclasses in other packages, while a default member is only accessible within the same package and not outside it, even though inheritance.

11. What happens if we declare a method as private?

A private method is only accessible within the same class. It cannot be inherited by subclasses and cannot be accessed directly from outside the class, ensuring better data hiding and security.

12. Which access specifier is the least restrictive?

public is the least restrictive access specifier. It allows class members to be accessed from any other class, whether in the same package or different packages.

13. What happens if a class member is declared without any access specifier?

If a class member has no access specifier, it gets default access. This means it is accessible only within the same package and not outside, even if inherited by a subclass in another package.

14. What is inheritance in Java?

Inheritance is a mechanism in Java that allows a class to acquire the properties and behavior of another class. It promotes code reusability and establishes a relationship between parent (superclass) and child (subclass) classes.

15. What is the syntax for inheritance in Java?

The syntax for inheritance uses the extends keyword:
class ChildClass extends ParentClass { // Child class code
}
Here, ChildClass inherits from ParentClass.

16. What are the types of inheritance in Java?

Java supports four types of inheritance: single, multilevel, hierarchical, and hybrid (through interfaces). Multiple inheritance is not supported with classes but can be achieved using interfaces.

17. What is single inheritance in Java?

Single inheritance is when a subclass inherits from a single superclass.
For example:
class Animal { }
class Dog extends Animal { }
Here, Dog inherits from Animal.

18. What is multilevel inheritance in Java?

Multilevel inheritance occurs when a class is derived from another derived class, forming a chain.
For Example: class A { }
class B extends A { }
class C extends B { }
Here, C inherits from B, which inherits from A.

19. What is hierarchical inheritance in Java?

In hierarchical inheritance, multiple subclasses inherit from a single superclass.
For Example:
class Animal { }
class Dog extends Animal { }
class Cat extends Animal { }
Here, both Dog and Cat inherit from Animal.

20. Why doesn’t Java support multiple inheritance with classes?

21. What is method overriding in Java?

22. What happens if a class is declared as final?

If a class is declared as final, it cannot be extended by any subclass. This is useful for creating immutable or security-sensitive classes, such as String in Java.

23. What is a Java library?

A Java library is a collection of precompiled classes and methods that provide reusable functionality for various tasks like data structures, mathematical operations, and networking, reducing the need to write code from scratch.

24. What is the java.lang package?

The java.lang package provides fundamental classes like String, Math, Integer, and System. It is automatically imported in every Java program, so you don’t need to explicitly import it.

25. What is the purpose of the java.util package?

The java.util package provides utility classes such as ArrayList, Collections, Date, and Scanner for handling data structures, time, and user input

26. What is the function of the java.sql package?

The java.sql package contains classes like Connection, Statement, and ResultSet to interact with databases using Java Database Connectivity (JDBC).

27. What is javax.swing used for?

The javax.swing package provides GUI components like JFrame, JButton, and JLabel for creating graphical user interfaces in Java applications.

28. What is the length() method in Java String?

The length() method returns the number of characters in a string.
Example:
String str = "Hello";
System.out.println(str.length()); // Output: 5

29. What does the charAt(int index) method do?

The charAt() method returns the character at a specified index in a string.
Example:
String str = "Java";
System.out.println(str.charAt(1)); // Output: 'a'

30. What is the purpose of toUpperCase()?

The toUpperCase() method converts all characters in a string to uppercase.
Example:
String str = "hello";
System.out.println(str.toUpperCase()); // Output: "HELLO"

31. What is the toLowerCase() method used for?

The toLowerCase() method converts all characters in a string to lowercase.
Example:
String str = "HELLO";
System.out.println(str.toLowerCase()); // Output: "hello"

32. What does substring(int beginIndex) do?

The substring() method extracts a part of a string from the given index to the end.
Example:
String str = "Hello";
System.out.println(str.substring(2)); // Output: "llo"

33. How does trim() work?

The trim() method removes leading and trailing spaces from a string.
Example:
String str = " Java ";
System.out.println(str.trim()); // Output: "Java"

34. How does substring(int beginIndex, int endIndex) work?

It extracts a substring from beginIndex to endIndex -1.
Example:
String str = "Hello";
System.out.println(str.substring(1, 4)); // Output: "ell"

35. What is the Math class in Java?

The Math class in Java is a final class in java.lang package that provides static methods for performing mathematical operations like exponentiation, rounding, trigonometry, and logarithms.

36. What does Math.max(int a, int b) do?

The Math.max() method returns the larger of two numbers.
Example:
System.out.println(Math.max(5, 9)); // Output: 9

37. How does Math.min(int a, int b) work?

The Math.min() method returns the smaller of two numbers.
Example:
System.out.println(Math.min(5, 9)); // Output: 5

38. What is the purpose of Math.sqrt(double a)?

The Math.sqrt() method returns the square root of a number.
Example:
System.out.println(Math.sqrt(25)); // Output: 5.0

39. What does Math.pow(double base, double exponent) do?

The Math.pow() method calculates the power of a number.
Example:
System.out.println(Math.pow(2, 3)); // Output: 8.0

40. How does Math.round(float a) work?

The Math.round() method rounds a floating-point number to the nearest integer.
Example:
System.out.println(Math.round(4.6)); // Output: 5

Chapter-2 HTML based web page covering basic tag


1. What is HTML?

HTML (HyperText Markup Language) is the standard language used to create and structure web pages.It uses various tags to format text, embed media, and structure content on the web.

2. What is the purpose of the html tag?

The html tag is the root element of an HTML document.It encloses all the content on the webpage, including metadata, scripts, and visual elements.

3. What is the use of the head tag?

The head tag contains metadata about the document, such as the title, character encoding, links to external resources (like stylesheets), and scripts.It does not display content directly on the webpage.

4. What is the function of the body tag?

The body tag contains all the visible content on the webpage, including text, images, videos, and other elements like forms and tables that users interact with.

5. What is the role of the title tag?

The title tag defines the title of the HTML document. It appears in the browser's title bar or tab.

6. What is the purpose of the a tag in HTML?

The a tag is used to create hyperlinks.
It allows users to navigate to another webpage, section, or resource when they click on the link.The target URL is specified in the href attribute.

7. How is an image embedded in HTML?

An image is embedded using the img tag, with the src attribute specifying the image file's location. The alt attribute provides alternative text for the image if it cannot be displayed.

8. What is the p tag used for?

The p tag defines a paragraph in HTML.
It is used to structure text into readable blocks.By default, paragraphs are displayed with some space above and below them.

9. What does the br tag do?

The br tag creates a line break in the text.It is useful for breaking lines of text within a paragraph or for forcing a new line without starting a new paragraph.

10. What is the difference between ul and ol ?

The ul tag is used for an unordered (bulleted) list, while the ol tag is used for an ordered (numbered) list. Both tags contain li elements to define each list item.

11. What is the li tag used for?

The li tag defines a list item.
It is used within the ul (unordered list) or ol (ordered list) to specify individual elements in the list.

12. What does the h1 to h6 tags represent?

The h1 to h6 tags represent heading elements in HTML.h1 is the highest level, typically used for the main title, while h6 is used for the smallest headings.

13. How is a table created in HTML?

A table is created using the table tag.Inside, the tr tag defines table rows, the th tag defines table headers, and the td tag defines table data (cells).

14. What is the purpose of the form tag?

The form tag is used to define a form in HTML.It enables users to input data, which can then be sent to the server for processing using various input elements like text fields and buttons.

15. What are input types in HTML?

HTML input types define the kind of data an input field should accept.Examples include text (input type="text"), password (input type="password"), and radio (input type="radio") fields.

16. What is the role of the alt attribute in the img tag?

The alt attribute provides alternative text for an image.
It describes the image when it cannot be displayed and helps with accessibility for visually impaired users and search engine indexing.

17. What is the hr tag used for?

The hr tag creates a horizontal rule (line) in a webpage. It is often used to separate sections or visually break content into distinct areas.

18. What is the purpose of the class attribute in HTML?

The class attribute is used to assign one or more class names to an HTML element.It is helpful for grouping elements and applying the same CSS styling or JavaScript functionality.

19. What is the id attribute used for?

The id attribute uniquely identifies an element in an HTML document.It is commonly used for applying CSS styles, targeting elements with JavaScript, or linking to specific sections via anchor links.

20. How do you create a dropdown list in HTML?

A dropdown list is created using the select tag. The options within the list are defined using option tags, allowing the user to select one item from the list.

21. What is the link tag used for?

The link tag is used to link external resources, such as stylesheets or icons, to an HTML document.It is typically placed in the head section of the document.

22. How do you include an external stylesheet in HTML?

An external stylesheet is linked to an HTML document using the link tag with the rel="stylesheet" attribute and the href attribute pointing to the CSS file's location.

23. What is the action attribute in a form?

The action attribute in a form specifies the URL where the form data will be sent for processing when the user submits the form.This is often a server-side script or a destination page.

24. What is the value attribute in HTML?

The value attribute specifies the value of an input element.For form elements like input, it defines the data that will be sent to the server when the form is submitted.

25. What is the difference between the button and input tags in HTML?

Both can be used to create buttons, but button is more flexible and can contain HTML content, while input type="button" is typically used for simple buttons and does not support inner HTML.

26. What is the use of the name attribute in HTML form elements?

The name attribute specifies a name for form elements.It is used to identify the form data after submission, as the data is sent with the name as the key.

27. How do you add a background color in HTML?

The background color of an HTML element can be set using the background-color CSS property.This can be applied either through inline styles, internal stylesheets, or external CSS.

28. How can you group radio buttons in HTML?

Radio buttons are grouped using the same name attribute.This ensures that only one button in the group can be selected at a time.

29. How do you add an external JavaScript file in HTML?

To add an external JavaScript file, use the script tag with the src attribute pointing to the file location.This is typically placed at the end of the body for performance reasons.

30. What is the caption tag used for in an HTML table?

The caption tag defines the title or caption of a table.It is placed inside the table tag and provides a description of the table’s contents.

31. What is the method attribute in an HTML form?

The method attribute defines how to send form data to the server.Common values are GET, which appends data to the URL, and POST, which sends data as part of the HTTP request body.

32. What does the align attribute do in HTML?

The align attribute is used to align elements like text, images, and tables.It can have values like left, right, center.

33. What is the difference between the src and href attributes?

The src attribute is used to specify the source of media files, like images or videos.The href attribute is used to define the destination URL for hyperlinks.