0% found this document useful (0 votes)
13 views4 pages

Chapter 2 Comp

Uploaded by

shivd5634
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views4 pages

Chapter 2 Comp

Uploaded by

shivd5634
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Part A: Very Short Questions

1. What is the implication of a public modifier?

The public modifier in Java is an access modifier that allows a class, method, or variable to be accessed from any other class or package. When a member is declared as public,

it can be accessed and used by any other class or object in the program.

2. If you use the private modifier before a data member, what does it mean?

When the private modifier is used before a data member in Java, it means that the member is only accessible within the same class. It cannot be accessed or modified directly

by other classes or objects. Private members are typically used to encapsulate data and provide controlled access to them through getter and setter methods.

3. How can you create a class data member?

To create a class data member in Java, you declare a variable within the class, but outside any method or constructor. This creates a member variable that is associated with

the class itself rather than specific instances of the class.

4. How can you initialise a class member within a class?

Class members can be initialized within the class using an initializer block or through assignment within the declaration itself

5. Which operator is used to create an object instance?

The "new" operator is used to create an object instance in Java

6. Which operator must be used to invoke the methods of an object?

The dot (.) operator is used to invoke methods of an object

7. Which return type must be used if the method does not return any value?

"void" return type

8. Classify the following as primitive or non- primitive data types:

(i) char - Primitive data type.

(ii) arrays - Non-primitive data type

(iii) int - Primitive data type.

(iv) classes - Non-primitive data type

9. Name the keyword which makes the variable as a class variable.

The "static" keyword is used to make a variable a class variable (also called a static variable).

Part B: Short Questions


1. Write short notes on class declaration and method declaration.

class declaration is used to define a blueprint for creating objects. It specifies the properties and behaviors that objects of the class will have.The basic syntax for a class

declaration is: public class ClassName { “ class members “ }

A method declaration is used to define a behavior or action that can be performed by an object of a class. The basic syntax for a method declaration is: returntype

method_name { “ parameter list “ }

2. Explain in brief about Access Modifiers in Java.

Access modifiers in Java are keywords that determine the accessibility or visibility of classes, variables, and methods within a program. Java provides four types of access

modifiers:

public: The public access modifier allows unrestricted access to the class, variable, or method from anywhere in the program.

private: The private access modifier restricts access to the class, variable, or method only within the same class. It is not accessible from other classes.

protected: The protected access modifier allows access to the class, variable, or method within the same class, subclasses, and classes in the same package.

default: If no access modifier is specified, the default access modifier is applied. It allows access to the class, variable, or method within the same package only.

3. How can you access data members and member methods through an object?

To access data members and member methods through an object in Java, you use the dot (.) operator.the syntax is objectName.dataMember;/ .methodName();

4. When will you use instance variables and class variables in a class?

Instance variables are declared within a class but outside any method, constructor, or block. They are associated with each instance (object) of the class and have separate

copies for each object.

Class variables are declared with the static keyword within a class but outside any method, constructor, or block. They are shared among all instances of a class and have only

one copy in memory. Class variables are associated with the class itself

5. How can you access class variables outside the class?

To access class variables outside the class, you use the name of the class followed by the dot operator and the name of the class variable. Since class variables are shared

among all instances of a class, you can access them without creating an object of the class.

6. What will you write in the parameter list of a method, if the method actually does not take any parameters?

Parameter List of a Method with No Parameters:

If a method does not take any parameters, the parameter list is left empty. It is represented by a pair of empty parentheses”()”.

7. Design a class RailwayTicket with following description:

Instance variables/data members:

String name: To store the name of the customer

String coach: To store the type of coach customer wants to travel

long mobno: To store customer's mobile number


int amt: To store basic amount of ticket

int totalamt: To store the amount to be paid after updating the original amount

Member methods:

void accept () - To take input for name, coach, mobile number and amount.

void update() - To update the amount as per the coach selected(extra amount to be added in the amount as follows)

Type of Coaches Amount

First AC 700

Second AC 500

Third AC 250

Sleeper None

void display() - To display all details of a customer such as name, coach, total amount and mobile number. Write a main method to create an object of the class and all the

above member methods.

import java.util.Scanner;

class RailwayTicket {

String name;

String coach;

long mobno;

int amt;

int totalamt;

void accept() {

Scanner sc=new Scanner;

System.out.println(“enter your name”);

name = sc.nextLine();

System.out.println(“enter your coach”);

coach = sc.nextLine();

System.out.println(“enter your mobile number”);

mobno = sc.nextLong();

System.out.println(“enter your amount”);


amt = sc.nextInt();

void update() {

amt=0;

System.out.println(“enter the another money if u want to add in add for another ticket”);

System.out.println(“here is the list”);

System.out.println(“just put the list from the book by urself”);

Scanner sc=new Scanner;

amt = sc.nextInt();

int tolalamt += amt;

void display() {

System.out.println(name);

System.out.println(coach);

System.out.println(totalamt);

System.out.println(mobno);

public static void main(String[] args) {

RailwayTicket ticket = new RailwayTicket();

ticket.accept();

ticket.update();

ticket.display();

You might also like