ch1 Part B
ch1 Part B
Object-Oriented Programs are slower than other programs, because of their size.
2. What is an instance?
Instance variables are specific to a particular instance of a class. For example, each time
you create a new class object, it will have its copy of the instance variables. Instance
variables are the variables that are declared inside the class but outside any method.
Class Object
A class is a group of similar objects. Object is a real-world entity such as book, car, etc.
Class can only be declared once. Object can be created many times as per
requirement.
The characteristics of object oriented programming are classes, objects, encapsulation, inheritance,
abstraction, and polymorphism.
Inheritance isa mechanism in which one object acquires all the properties and behaviors of a parent
object. Advantages are it can save time and effort as the main code need not be written again;
provides a clear model structure which is easy to understand; leads to less development and
maintenance.
6. What is bytecode?
Bytecode is simply a set of instruction that occurs between the compiler (low level language) to
JVM(high level language).it is instruction set for JvM(Java virtual machine)
Java is platform-independent because it uses a virtual machine and programs written in Java can
be run on multiple platforms without re-writing them individually for a particular platform
Char:- ‘L’
Float:-‘6.99’
9. Define the term data type List the different data types in Java
the type of value a variable has and what type of mathematical, relational or logical
operations can be applied without causing an error.
Primitive data types- boolean, char, byte, short, int, long, float and double.
A variable's scope is the place in your program where it can get variable declaration. A variable that
can be accessed in 'any' scope can be accessed absolutely anywhere in your program.
11. Determine which of the following are valid literals with reasons and type
(1) 0.5 This is a valid literal. It represents a floating-point number and has a type of float.
(ii) 9.3e12 This is a valid literal. It represents a floating-point number in scientific notation and has a
type of float.
(vi) 8.15 PM" This is not a valid literal. It appears to be a time representation, but literals typically
refer to values in programming languages, and this format is not commonly used as a literal. It does
not have a specific type associated with it.
(iii) 27,822 This is not a valid literal. The presence of a comma (,)
(iv) ‘a' This is a valid literal. It represents a character literal enclosed in single quotes. it is considered
a char
(v) ‘/n’ This is not a valid literal. It appears to be attempting to represent a newline character.
(vii) 20543 This is a valid literal. It represents an integer value and has a type of int
An expression is a sequence of operators and operands. Expressions can be used for many purposes
like computing value from the operands, invoking functions or updating Values. They appear in
direction left-to-right.
java. lang
Vol = 3.1459 * r * r * h / 3;
(ii) Fn = 0.5 if x <= 30 otherwise Fn = 0
double Fn;
if (x <= 30) {
Fn = 0.5;
} else {
Fn = 0;
The nested if statement in Java is a set of if conditions one within another. The inner if conditions
are only executed when the outer if condition results in true
one block
char tickettype;
if(tickettype == A')
System.out.println("AC ticket");
System.out.println("Unreserved ticket");
char tickettype;
switch(tickettype)
Case(‘A’):
System.out.println("AC ticket");
break;
Case(‘2’):
Break;
Case(‘U’):
System.out.println("Unreserved ticket");
Break;
18. Give the output for the following code and remove errors (if any)
if ( x == 10)
System.out.println ("true");
else
System.out.println("false");
Correction non
When x=10
True
When x=15
false
(a) 25
(b) 30
(c) 12
void main(){
int p;
if(p% 15==0)
System.out.println("divisible by 15");
else
System.out.println("divisible by 5 only");
else
A:- Divisible by 5
B:- Divisible by 15
C:- Divisible by 5
if (aNum >= 0)
System.out.println("third string");
Output
second string
third string
19(1) Complete the following program and run it by initializing the char with different values of
variable ch
System.out.println("_________”);
else
System.out.println("_________”);
else
System.out.println("_________”);
else
System.out.println("_________”);
if ((ch <= '8' && ch >= '0') || (ch <= 'Z' && ch >= 'A') || (ch <= 'z' && ch >= 'a')) {
if ((ch <= 'Z' && ch >= 'A') || (ch <= 'z' && ch >= 'a')) {
System.out.println("Uppercase letter");
} else {
System.out.println("Lowercase letter");
} else {
System.out.println("Digit");
} else {
System.out.println("Special character");
(ii) Complete the following program code to define the class, main() method and also intialise the
value of a variable tickettype to A, 2 or U.
Class _____________
Public ____________
char tickettype=__________
if(tickettype== 'A')
System.out.printin(" AC ticket");
else if (tickettype=='2')
else if (tickettype='U')
System.out.println("Unreserved ticket");
}
}
class TicketType {
if (tickettype == 'A') {
System.out.println("AC ticket");
System.out.println("Unreserved ticket");
It is a process of hiding all the main details from the user and showing only the functionality to the
user.
The value is not Stored yet cause there is an syntax error you cannot store a character in int u need
to add char instead of int
output:- 65
system.out.println("OF LUCK");
Choose the correct option for the output of the above statements
*(ii) BEST
OF LUCK
*Option no. 2
log()
double or float both are correct but I prefer you to write double
32
-5
(ii)Math.ceil(3.4)+ Math.pow(2, 3)
12
If (var==1)
System.out.println("good");
else if(var==2)
System.out.println("better");
else if(var==3)
System.out.print("best");
else
System.out.println("invalid");
Switch (var)
case 1:
System.out.println("good");
break;
case 2:
System.out.println("better");
break;
case 3:
System.out.println("best");
break;
default:
System.out.println("invalid");
if (bill>10000)
else
discount=bill*5.0/100;
discount = (bill > 10000) ? (bill * 100 / 100) : (bill * 5.0 / 100);
29. Give the output of the following program segment and also mention how many times the loop is
int i;
System.out.println(i);
System.out.println(i*4);
Unary operators perform an action with a single operand. Binary operators perform actions with two
operands.
32. Write the memory capacity (storage size) of short and float data type in bytes
short 2 bytes
float 4 bytes
(iii)== =operator
(iv){} =separator
if-else if
(ii) Which statement will be executed depend upon the output of the expression inside if statement.
switch-case
(x*2)+(2*x*y);
36. Write the return data type of the following functions: random()
double
37. If the value of basic-1500, what will be the value of tax after the following statement is
executed?
tax = basic>12007?200:100;
200
38. Give the output of following code and mention how many times the loop will execute?
Int i;
if(i%2==1)
continue;
System.out.print(i+" ");
16
40. Evaluate the following expression if the value of x=2, y=3 and z=1.
V=x+--z+y++ +y
2+0+3+4
=9
Syntax Error.
Runtime Error.
Logical Error.
42. What is meant by a package? Give an example.
A package is an organized collection of classes which is included in the program as per the
requirement of the program. For example java.io package is included for input and output
operations in a program.
Java is a versatile programming language that supports several programming approaches,but the
mast common one is “Object-oriented programming (OOP) “which Java is often used for object-
oriented programming, which emphasizes encapsulation, inheritance, and polymorphism. Java's
class and interface constructs make it easy to create reusable and modular code.
An object is an identifiable entity with some characteristics and behaviour of its own. You are
surrounded by many objects. An object in the real world can be anything that can be seen or
tangible like a flower, a book, a newspaper. The characteristics of an object are also known as state,
attributes or data. The behaviour are the operations that can be performed on it. Real world objects
share both aspects: state and behaviour
A class is a blueprint of objects that share common properties, relationships and behaviours. A class
is defined using the keyword 'class'.
❖Member functions(methods)
Classes encapsulate all the required properties of the objects into a single unit called data members.
The functions that operate on these data are called methods or member functions. Since classes use
the concept of data abstraction, they are also known as Abstract Data Types (ADT).
The Object-Oriented Programming (OOP) approach in Java has several advantages. OOP approach in
Java promotes modularity, flexibility, and extensibility, which can lead to more efficient and
maintainable code.Here are three main advantages:
Encapsulation
Inheritance
Polymorphism
6. The data structure for given below. What can be the methods for the object?
Colour
getRed()
weight
69kg
Model Year
The state or attributes are the built in characteristics or properties of an object. For example, aksh
has the fat ,short height etc. Behaviour of the object - The behavior or operations of an object are its
predefined functions. For example aksh can sleep,watch hentai etc
Method overloading is a feature of Java in which a class has more than one method of the same
name and their parameters are different.
A compound statement is any number and kind of statements grouped together within curly braces.
For example
The if-else statement helps you to run a specific block of a program if the condition is true or else, it
will check other conditions.
if(Boolean_expression)
When a complete if or if-else statement is written within another if statement, it is known as nesting
of the if statement. The nested block can be within the if block or inside the else block. The nesting
can be done to any level. but make sure that unnecessary complications are avoided.
Flow chart at pg 20 and 21
loops are used to repeat a block of code. For example, if you want to show a message 100 times,
then rather than typing the same code 100 times, you can use a loop.
1st Division
16. Write a program to convert seconds into corresponding number of hours, min and seconds. For
example, 7266 sec =2hrs, 1 min, 6 sec.
import java.util.Scanner;
class SecondsConverter {
17. Write a program to check whether a given number is positive or negative. If positive, then find
whether even or odd,
import java.util.Scanner;
class Num {
if (num > 0) {
if (num % 2 == 0) {
System.out.println("It is even.");
} else {
System.out.println("It is odd.");
} else {
18. Write a menu driven program to display the pattern as per user's choice.
Pattern 1
ABCDE
ABCD
ABC
AB
A
Pattern 2
LL
UUU
EEEE
Letters Unicode
A 65
B 66
. .
. .
. .
Z 90
12
1 23
1234
12345