0% found this document useful (0 votes)
7 views9 pages

Computers Notes

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

Computers Notes

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

Computer Science- Term II –Study Material Grade VIII

An act of using essential features without including background details is called Data
Abstraction.
In an Object Oriented Programming, the stress is given on data.
Wrapping of data and function together as a single unit is called Encapsulation.
The process by which a class acquires the property of another class is known as inheritance.
1. 2. 3. 4.
5. In Object Oriented Programming, using a function for many purposes is termed as
polymorphism.
6. Compilers and Interpreters are referred to as Language processors.
It converts the whole source program into the object program at once.
It converts the source program into the object program, one line at a time.
Compiler
Interpreter
It displays the errors for the whole program together, after the compilation.
It displays the error one line at a time and only after fixing that error the control goes to the next
line.
7. Write three benefits of Object Oriented Programming. Three benefits of Object Oriented
Programming are:
8. 9.
1. The reusability of the program code is enhanced.
2. Data abstraction makes the software easier to handle.
3. Software for complex tasks can be easily developed.
James Gosling developed Java programming language.
JVM is Java Virtual Machine
10. Four features of Java are:
1. It is an Object Oriented Programming Language.
2. It is platform independent. It provides us Write Once, Run Anywhere (WORA) feature.
3. It uses a compiler as well as an interpreter.
4. It is case sensitive.
11. Define the following: (a) A compiler
A compiler is a program that translates a source program written in some high-level
programming language into a target program in another low-level programming language
without changing the meaning of the program. The compiler processes the complete source
program at once and if there are compilation errors, they are all reported together at once.

(b) interpreter
An interpreter is a program that reads a source program line by line, converts each line into its
equivalent machine code and executes it. As it reads the program line by line so the errors are
reported one by one.
(c) Byte code
Java compiler converts Java source code into an intermediate binary code called Bytecode.
Bytecode can't be executed directly on the processor. It needs to be converted into Machine
Code first by the JVM(Java Interpreter).
12. What is Java Virtual Machine (JVM)?
Java Virtual Machine (JVM) is a software that takes Bytecode as input, converts it into Machine
code of the specific platform it is running on and executes it. JVM is platform specific, each
platform has its own JVM.
13. What are Java reserved words? Name any five.
In Java, a reserved word is a word that has a predefined meaning in the language. Due to this,
reserved words can’t be used as names for variables, methods, classes or any other identifier.
Reserved words are also known as keywords. Five commonly used Java reserved words are:
1. public 2. class 3. int
4. double 5. char
14. Name a package that is invoked by default.
java.lang
15. Java is a case sensitive. Explain.
Java is case sensitive means that it distinguishes between upper case and lower case
characters. Consider the below code snippet:
int studentMarks; StudentMarks = 85;
This will give a compilation error as Java will treat studentMarks and StudentMarks as two
different variables because the case of the characters is not same in both.
16. The main function in a Java program is declared as: public static void main (String args[])
What is the significance of the words public, static and void?
public — The public keyword is an access specifier. It controls the visibility of class members.
We can access public class members outside the class where we declare them. We need to
make the main method public because it will be called by code outside of its class when the
program is started.
static — When we declare a method inside a class as static, we can call it without creating the
object of that class. We need to make the main method static because Java Virtual Machine
(JVM) will call it to start the program even before any objects of the class are created.
void — The void keyword tells the compiler that the main method will not return any value.

17. Java program uses compiler as well as interpreter. Explain.


Java compiler compiles Java source code to Bytecode. Bytecode cannot run on the processor
directly as processor only understands Machine Code. Java Virtual Machine (JVM) takes this
Bytecode as input and converts it into Machine Code line by line. So, JVM acts as an interpreter
for converting
Bytecode to Machine Code. In this way, a Java program uses both a Compiler as well as an
Interpreter to get executed on the processor.
18. 19. 20.
22. Mention five states (characteristics) and two methods for the following Class Employee:
A Class is also considered as an object factory.
The term instantiation is used for creating various objects.
new keyword indicates an operator for dynamic allocation of an object.
21. Define Object with an example.
An object is an entity having a specific identity, specific characteristics and specific behavior.
Taking
a car as an example of an object, it has characteristics like colour, model, version, registration
number, etc. It has behaviours like start the engine, stop the engine, accelerate the car, apply
the brakes, etc.
Characteristics
Methods
Name
Employee Number
Income Tax
computeSalary()
computeTax()
Pan Number
Salary
23. What does the following statement mean?
Employee staff = new Employee ( );
This statement creates a new object of class Employee. The newly created object is assigned
to a variable named staff which is of Employee type. The object can be accessed using staff
variable.
24. What do you understand by Token? Name different types of tokens.
A token is the smallest element of a program that is meaningful to the compiler. The different
types of tokens in Java are:
1. Identifiers(class name, variables,object names,function names)
2. Literals(constants)
3. Operators(arithmetic,relational,logical)
4. Separators({})
5. Keywords(int, class,void)

25. What are the rules to assign a variable in a Java programming?


1. Name of the variable should be a sequence of alphabets, digits, underscore and dollar sign
characters only.
2. It should not start with a digit.
3. It should not be a keyword or a boolean or null literal.
26. Write the Java expressions for the following:
i. p=a2+bc
p=a*a+b*c
ii.m=a2 -b2 /(ab)
m = (a * a - b * b) / (a * b)
iii. s = ut + (1/2)at2
s = u * t + (1.0 / 2) * a * t * t
iv. f = uv / (u + v)
f = u * v / (u + v)
v. (a + b)2 + b
(a + b) * (a + b) + b
vi. y = 2(lb + bh + lh)
y = 2 * (l * b + b * h + l * h)
27. What is an operator?
An operator is a symbol or sign used to specify an operation to be performed in Java
programming.
28. Name the different types of operators and explain.
The different types of operators are Arithmetical, Logical and Relational.
(a) Arithmetical operator
Arithmetic operators are used to perform mathematical operations on its operands. int a = 10 +
20;
+,-,*,/,%
(b) Relational operator
Relational operators are used to determine the relationship between the operands. Relational
operators compare their operands to check if the operands are equal to ( == ), not equal to ( !
= ), less than ( < ), less than equal to ( <= ), greater than ( > ), greater than equal to ( >= ) each
other. The result of an operation involving relation operators is a boolean value — true or false.
Example:
int a = 8;
int b = 10;
boolean c = a < b;
Here, as a is less than b so the result of a < b is true. Hence, boolean variable c becomes true.

(c) Logical operator


Logical operators operate on boolean expressions to combine the results of these boolean
expression into a single boolean value.
Example:
int a = 7;
int b = 10;
boolean c = a < b && a % 2 == 0;
Here, the result of first boolean expression a < b is true and the result of second boolean
expression a % 2 is false. The logical AND operator ( && ) combines these true and false
boolean values and gives a resultant boolean value as false. So, boolean variable c becomes
false.
(d)
Arithmetical Operator
Arithmetic operators are used to perform mathematical operations.
+, -, *, /, etc. are a few examples of Arithmetic operators.
Logical Operator
Logical operators operate on boolean expressions to combine the results of these boolean
expression into a single boolean value.
&&, ||, ! are a few examples of Logical Operators
e) Logical AND (&&) and Logical OR(||)
It evaluates to true only if both of its operands are true.
It evaluates to true if one or both of its operands are true.
Logical AND (&&)
Logical OR(||)
Example:
int a = 8, b = 13, c = 0; if (a > 10 && b > 10)
Example:
int a = 8, b = 13, c = 0; if (a > 10 || b > 10)
c = 10; else
c = 5;
Here, value of c will be 5 as one of the operands is false.
c = 10; else
c = 5;
Here, value of c will be 10 as at least one of the operands is true.
f. System.out.print( ) and System.out.println( )
System.out.print( )
System.out.println( )
It prints data to the console but the cursor remains at the end of the data in the same line.
It prints data to the console and places the cursor in the next line.
Next printing takes place from the same line.
Next printing takes place from next line.

g. State the difference between = and ==.


=
==
It is the assignment operator used for assigning a value to a variable.
It is the equality operator used to check if a variable is equal to another variable or literal.
E.g. int a = 10; assigns 10 to
E.g. if (a == 10) checks if variable a is
variable a.
equal to 10 or not.
h .package needed to import scanner class
java.util
i.method that accepts a character through scanner object
charAt()
j.method to accept an exponential value through scanner object
nextDouble()
k. to accept a fractional value (float) 'm' through Scanner Class
Scanner in = new Scanner(System.in); float m = in.nextFloat();
nextInt( )
nextFloat( )
Scans the next token of input as an int
Scans the next token of input as a float
Syntax Errors
Logical Errors
Syntax Errors occur when we violate the rules of writing the statements of the programming
language.
Program fails to compile and execute.
Logical Errors occur due to our mistakes in programming logic.
Program compiles and executes but doesn't give the desired output.
Logic errors need to be found and corrected by the people working on the program.
Syntax Errors are caught by the compiler.
l. nextInt( ) and nextFloat( ) methods
m. Syntax and logical errors

n.
Java Programs:
1. public class datatypes {
public static void main(String args[]) {
int i=10,j=90;
float f=90.8888f;
double d=90.88888888888;
char ch='9';
boolean b=true;
String str="hello a23"; System.out.println("int"+i+"float"+f+"double"+d+"char"+ch+"string"+str);
}}
What is the use of the keyword import?
import keyword is used to import built-in and user-defined packages into our Java program.
2. A person is paid ₹350 for each day he works and fined ₹30 for each day he remains absent.
Write a program to calculate and display his monthly income, if he is present for 25 days and
remains absent for 5 days.
public class Salary {
public static void main(String args[]) { int salary = 25 * 350;
int fine = 5 * 30;
int netSalary = salary - fine;
System.out.println("Monthly Income = " + netSalary); }
}
3. In a competitive examination, there were 150 questions. One candidate got 80% correct and
the other candidate 72% correct. Write a program to calculate and display the correct answers
each candidate got.
public class Exam {
public static void main(String args[]) {
int totalQuestions = 150;
int c1 = (int)(80 / 100.0 * totalQuestions);

5. A shopkeeper offers 10% discount on the printed price of a Digital Camera. However, a
customer has to pay 6% GST on the remaining amount. Write a program in Java to calculate
the amount to be paid by the customer taking printed price as an input.
import java.util.Scanner; public class CameraPrice {
public static void main(String args[]) {
Scanner in = new Scanner(System.in); System.out.println("Enter printed price of Digital
Camera:"); double mrp = in.nextDouble();
double disc = mrp * 10 / 100.0;
double price = mrp - disc;
double gst = price * 6 / 100.0;
price += gst;
System.out.println("Amount to be paid: " + price);
}}
6. Write a Java program to accept a side of the square, length and breadth of a rectangle by
passing parameters in the main and print area and perimeter of square and rectangle.
public class area {
public static void main(int l,int b, double s)//passing parameters {
int area_rect=l*b;
int c2 = (int)(72 / 100.0 * totalQuestions); System.out.println("Correct Answers of Candidate 1 =
" + c1); System.out.println("Correct Answers of Candidate 2 = " + c2);
}}
4. The normal temperature of human body is 98.6°F. Write a program to convert the
temperature into degree Celsius and display the output.
Hint: c / 5 = f - 32 / 9
public class Celsius {
public static void main(String args[]) {
double f = 98.6;
double c = 5 * (f - 32) / 9.0; System.out.println("Temperature in Degree Celsius = " + c);
}}

int perimeter_rect=2*(l+b);
double sq_area=s*s;
double sq_peri=4*s;
System.out.println(“area of rect”+area_rect+"perimeter of rect"+perimeter_rect+"area of
square"+sq_area+"Perimeter of square"+sq_peri); }
}
7. Write a Java program to accept radius of the circle and print area and circumference of a
circle.
import java.util.*; public class areacircle {
public static void main(String args[])//passing parameters {
Scanner sc=new Scanner(System.in);
int radius=sc.nextInt();
double area=3.14*r*r;
double circumference=3.14*2*r;
System.out.println(“area of circle is”+area+”Circumference is”+circumference);
1.
Write a Java program to check whether a person is eligible to vote and display the appropriate
message.
(age>=18 –eligible to vote)
}}
if - else statement is used to execute one set of statements when the condition is true and
another set of statements when the condition is false. It has the following syntax:
if (condition 1) { Statement a; Statement b; ..
} else {
Statement c; Statement d; ..
}
import java.util.Scanner; public class vote
{
public static void main(String args[]) {
Scanner in = new Scanner(System.in); System.out.println("Enter the age of person "); int age=
in.nextInt();
if(age>=18)
{
System.out.println(“Eligible to vote”); }

else
{
System.out.println(“Not eligible to vote”); }
}
}
2. Write a Java program to accept a number and check whether its an even or odd. Print an
appropriate message.
import java.util.Scanner; public class vote
{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.println("Enter the age of person "); int num= in.nextInt();
if(num%2==0)
{
System.out.println(num+“ is even”); }
else
{
System.out.println(num+” is odd”); }
}
}
3. Write a Java program to accept a non zero number and check whether it’s a positive or
negative. Print an appropriate message.
import java.util.Scanner; public class vote
{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.println("Enter the age of person ");
int num= in.nextInt(); if(num>0)
{
System.out.println(num+“ is positive”); }
else
{
System.out.println(num+” is negative”); }
}
}

You might also like