0% found this document useful (0 votes)
106 views8 pages

Java Basics for Class 8 Students

The document provides an introduction to Java programming using BlueJ. It includes fill-in-the-blank questions, true/false questions, multiple choice questions, and sample code snippets to print output. The questions cover topics like classes, objects, variables, operators, and basic Java concepts. Sample programs demonstrate how to print text, calculate sums and averages, swap variable values, and generate a bill.
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)
106 views8 pages

Java Basics for Class 8 Students

The document provides an introduction to Java programming using BlueJ. It includes fill-in-the-blank questions, true/false questions, multiple choice questions, and sample code snippets to print output. The questions cover topics like classes, objects, variables, operators, and basic Java concepts. Sample programs demonstrate how to print text, calculate sums and averages, swap variable values, and generate a bill.
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

Introduction to Java & BlueJ

Class 8 - Logix Kips ICSE Computer with BlueJ


Fill in the blanks

Question 1

The Java programming language is an Object Oriented Programming language.

Question 2

Encapsulation is the technique of binding both data and functions together.

Question 3

A Class is a blueprint that defines data and functions common to all objects of a certain kind.

Question 4

The smallest meaningful element of a Java program is called a Token.

Question 5

Initialization is a process of assigning some initial value to a variable.

State True or False

Question 1

Polymorphism allows a function to behave differently for different objects.


True

Question 2

Constants mean the fixed values that do not change during the execution of a program.
True

Question 3

The main() function indicates that the execution of the Java program will begin from this point.
True
Question 4

Every Java statement must end with a colon.


False

Question 5

Inheritance is a feature using which an object in one class acquires the properties of another
class.
True

Question 6

While comparing two variables, their data types should not be the same.
False

Application Based Questions

Question 1

Ritika has written a program in Java. After the successful compilation of her program, which
option should she select to run it?

Answer

To run her program, Ritika needs to right-click on the class icon and select void main(String[]
args) option from the pop-up menu.

Question 2

Kareena is writing a program in Java to calculate the average of three subjects. Which operators
will she use for the same?

Answer

Kareena will use the addition operator (+) to first calculate the total marks obtained in all three
subjects. After that, she will use the division operator (/) to divide the total marks by 3 to get the
average.

Multiple Choice Questions

Question 1

A .......... is a named location in the memory, which stores data temporarily.


1. Identifier
2. Keyword
3. Variable ✓

Question 2

What does OOP mean?

1. Object Oriented Programming ✓


2. Object Oriented Procedure
3. Object Origin Program

Question 3

Which among the following feature is used to manage the complexity of the system?

1. Abstraction ✓
2. Polymorphism
3. Encapsulation

Question 4

A Java program is compiled into an intermediate language called ..........

1. Source Code
2. Bytecode ✓
3. None of these

Question 5

The .......... works on a single variable or constant.

1. Unary Operator ✓
2. Arithmetic Operator
3. Relational Operator

Question 6

Which symbol is used to combine two or more items in a single line?

1. ?
2. + ✓
3. !
Answer the following

Question 1

Define objects. Give a real life example to explain objects along with their attributes and
behaviour.

Answer

An object represents data (attributes) and its related methods (behaviour) as a single unit. Take
the example of a car. When we describe a car as an object, there are a number of physical factors
which contribute to define its state such as its brand name, colour, speed, size, number of seats,
etc. In addition, the car has several functional definitions. It transports people to different
locations by controlling its speed and direction, an accelerator to increase or decrease its speed
and brakes to stop it. Thus we can say that car is an object that combines its attributes and
behaviour as a single unit.

Question 2

What is class?

Answer

A class is a blueprint to create objects. It defines data and functions common to all objects of a
certain kind.

Question 3

What do you understand by the term Polymorphism?

Answer

The word Polymorphism means "many forms". Polymorphism helps the programmer to use the
same function name for more than one purpose.

Question 4

What are operators? Why do we need them?

Answer

Operators are special symbols that are used to perform calculations. We need operators to form
expressions by applying them to the variables and constants.

Question 5
What do you understand by the term Keyword?

Answer

Keywords are reserved words that have a special meaning for the Java compiler. Java compiler
reserves these words for its own use so Keywords cannot be used as identifiers. For example,
void, public, class, int, etc.

Question 6

What are variables? How are they different from Constants?

Answer

A variable is a named location in the memory which stores data temporarily. A variable has a
unique name, a type and a size that is used to identify it in a program.

Difference between variables and constants is that the value of variables can change during the
execution of the program whereas value of constants are fixed and does not change during
program execution.

Lab Session

Question 1

Write a program to print, "Welcome to the World of Computers".

public class KboatWelcome


{
public static void main(String args[]) {
[Link]("Welcome to the World of Computers");
}
}

Output

Question 2

Write a program to print your Name, Class, Roll_No, Marks and Age. Name this file,
`MyProfile'.

public class MyProfile


{
public static void main(String args[]) {
[Link]("Name: Sneha Nayak");
[Link]("Class: 8C");
[Link]("Roll No: 24");
[Link]("Marks: 87");
[Link]("Age: 15");
}
}

Output

Question 3

Write a program to print your School name four times in separate lines.

public class KboatSchoolName


{
public static void main(String args[]) {
[Link]("St. Francis' College");
[Link]("St. Francis' College");
[Link]("St. Francis' College");
[Link]("St. Francis' College");
}
}

Output

Question 4

Write a program to find the sum and average of three numbers.

public class KboatSumAvg


{
public static void computeSumAvg(int a, int b, int c) {
[Link]("The three numbers are "
+ a + ", " + b + ", " + c );
int sum = a + b + c;
double avg = sum / 3.0;
[Link]("Sum = " + sum);
[Link]("Average = " + avg);
}
}

Output

Question 5

Write a program to interchange the value of two numbers without using the third variable.

public class KboatSwap


{
public static void swapNumbers(int a, int b) {
[Link]("The numbers are:");
[Link]("a=" + a + " b=" + b);
a = a + b;
b = a - b;
a = a - b;
[Link]("The numbers after interchange:");
[Link]("a=" + a + " b=" + b);
}
}

Output

Question 6

Write a program to calculate the compound interest.

public class KboatInterest


{
public static void computeInterest(double p, double r, int t) {
double amt = p * [Link](1 + (r / 100 ), t);
double interest = amt - p;
[Link]("Compound Interest = " + interest);
}
}

Output
Question 7

Write a program to calculate the tax for a taxable income of Rs. 4,10,000, if the tax rate is fixed
at 3.2%.

public class KboatTax


{
public static void main(String args[]) {
int income = 410000;
double rate = 3.2;
double tax = income * rate / 100;
[Link]("Tax = " + tax);
}
}

Output

Question 8

Create a program that will generate a bill at McDonald's for four vegetable burgers (@ Rs 45 per
vegetable Burger) and three vegetable McPuffs (@ Rs 25 per vegetable McPuff). There is a
special Independence Day discount of Rs 50 on the final bill amount.

public class KboatMcDBill


{
public static void main(String args[]) {
int vegBurgerCost = 4 * 45;
int vegMcPuffCost = 3 * 25;
int total = vegBurgerCost + vegMcPuffCost;
int amt = total - 50;
[Link]("4 Vegetable Burgers @ 45 = " +
vegBurgerCost);
[Link]("3 Vegetable McPuffs @ 25 = " +
vegMcPuffCost);
[Link]("Total = " + total);
[Link]("Discount = 50");
[Link]("Final Bill = " + amt);
}
}

Output

Common questions

Powered by AI

Encapsulation in Java involves bundling data and methods that operate on that data within a single unit, typically a class, and restricting access to some of the object's components. This prevents external interference and misuse, thus securing the data. By exposing only a controlled interface, encapsulation increases robustness by ensuring that an object can be modified or extended without affecting other parts of the program. This control layer ensures that changes to an object’s state are managed in a controlled environment .

Variables and constants serve distinct roles in Java programs. Variables are named locations in memory that store data temporarily, allowing their values to be changed or manipulated during program execution. In contrast, constants have fixed values that do not change once they are defined, providing stability and predictability throughout the program's lifecycle. By using constants, code becomes more readable and less error-prone, as it signifies an unchanged value explicitly .

BlueJ is an Integrated Development Environment (IDE) designed to support beginner Java programmers by providing a simple interface and tools that are particularly suited for learning object-oriented programming. It offers features like code visualization, interactive objects, and easy-to-use project management. These features aid beginners in understanding the fundamental concepts and mechanics of Java in a more accessible way compared to more complex IDEs .

Polymorphism in Java allows a single method or function to behave differently based on the object it is acting upon. This feature enhances flexibility by enabling the programmer to write code that can work with various data types and structures in a unified way. For example, the same function could handle different data objects and return results accordingly. This improves maintainability, as changes in object handling are encapsulated within polymorphic functions, reducing the need for extensive rewriting when extending functionalities .

Inheritance allows a new class, known as a subclass, to inherit properties and behaviors (methods) from an existing class, promoting code reusability by enabling developers to build on existing logic without redundant code. This reduces redundancy and improves maintainability. However, misuse of inheritance can lead to code that is tightly coupled and difficult to manage, especially when superclass modifications are required. This trade-off requires careful planning of class hierarchies .

Operators in Java are used to perform computations on variables and constants, forming expressions that dictate the computational logic of a program. They enable various operations such as arithmetic calculations, object comparisons, and data manipulations. The careful selection and use of operators affect the program’s efficiency, readability, and correctness. Proper operator usage is critical for constructing logical expressions that underpin the program’s workflows .

Abstraction in Java manages system complexity by exposing only necessary parts of an object's implementation while hiding intricate details. This approach simplifies interactions with complex systems by allowing the user to focus on high-level functionalities without getting bogged down by detailed inner workings. Abstraction provides a layer that enables future-to-object interactions through a simplified interface, facilitating maintenance and scalability .

An object in Java encapsulates data (attributes) and methods (behaviors) as a single unit. This concept can be illustrated with a real-world analogy: a car. The attributes of a car include its brand, color, and number of seats, while behaviors include accelerating and braking. Just as these attributes and behaviors are bundled to define the car's operation, Java objects encapsulate similar elements to model complex systems in a manageable way .

The main() function marks the entry point of a Java program and is integral for execution as it defines where the program should begin its operation. Positioned within a public class, it allows the Java Runtime Environment to start executing the workflow of the application from its contained statements. It serves as a universal starting convention that ensures consistency across Java applications, making it easier for developers to navigate and understand program flows .

Tokens are the smallest meaningful elements of a Java program, such as keywords, identifiers, and symbols. Understanding tokens is essential because they constitute the basic building blocks of the language. Recognizing how to effectively use tokens helps in constructing syntactically correct programs and in deciphering error messages related to compilation, which often stem from improper token usage. Comprehension of tokens is foundational for learning Java programming .

You might also like