0% found this document useful (0 votes)
33 views

ch1 Part B

Uploaded by

shivd5634
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
33 views

ch1 Part B

Uploaded by

shivd5634
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 19

Part B: Very Short Questions

1. Give two disadvantages of OOP approach.

Object-Oriented Programs are slower than other programs, because of their size.

Object-Oriented Programs are much larger than other programs.

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.

3. What is the difference between a class and an object?

Class Object

A class is a group of similar objects. Object is a real-world entity such as book, car, etc.

Class is a logical entity. Object is a physical entity.

Class can only be declared once. Object can be created many times as per
requirement.

4. State the main characteristics of OOPS.

The characteristics of object oriented programming are classes, objects, encapsulation, inheritance,
abstraction, and polymorphism.

5 What is inheritance? Write its benefits

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)

7. How is Java platform independent?

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

8. Give an example of char and floating type constants

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.

Non-primitive data types:- Classes, Interfaces, and Arrays.

10. What do you mean by scope of a variable?

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

12. What are expressions? In what direction are they evaluated?

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.

13. Which package supports the mathematical functions in Java?

java. lang

14. Write Java equivalent expressions for the following:

(1) Vol 3.1459 r2 h/3

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;

(iii) To compute the sum of x20 and x

int sum = x + x20;

15. What is nested if ?

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

16. In a concatenated if statement, how many blocks will get executed?

one block

17. Rewrite the following program code using switch case

char tickettype;

if(tickettype == A')

System.out.println("AC ticket");

else if (tickettype == ‘2’ )

System.out.println("Second class Sleeper ticket");

else if (tickettype ‘ U' )

System.out.println("Unreserved ticket");

char tickettype;

switch(tickettype)

Case(‘A’):
System.out.println("AC ticket");

break;

Case(‘2’):

System.out.println("Second class Sleeper ticket");

Break;

Case(‘U’):

System.out.println("Unreserved ticket");

Break;

18. Give the output for the following code and remove errors (if any)

(i) Assuming the value of x is 10 and 15.

if ( x == 10)

System.out.println ("true");

else

System.out.println("false");

Correction non

When x=10

True

When x=15

false

(ii) Assume the values of p is

(a) 25

(b) 30

(c) 12

void main(){

int p;

p =25;// code checking with first sample value


if(p % 5 ==0)

if(p% 15==0)

System.out.println("divisible by 15");

else

System.out.println("divisible by 5 only");

else

System.out.println("Not divisible by 5"); }

A:- Divisible by 5

B:- Divisible by 15

C:- Divisible by 5

(iii) Assume the value of aNum is 3 .

if (aNum >= 0)

if (aNum== 0) System.out.println("first string");

else System.out.println("second string"); }

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

public class Nestedif {

public static void main(String args[]){


char cha'#': // variable initialized with a character

if((ch<=56 && ch>=48) || (ch<=91 && ch>=65) || (ch<=123 && ch>=97))

if((ch<=91 && ch>=65)||(ch<=123 && ch>=97))

if(ch<=91 && cha=65)

System.out.println("_________”);

else

System.out.println("_________”);

else

System.out.println("_________”);

else

System.out.println("_________”);

public class NestedIf {

public static void main(String[] args) {

char ch = 'A'; // variable initialized with a character

if ((ch <= '8' && ch >= '0') || (ch <= 'Z' && ch >= 'A') || (ch <= 'z' && ch >= 'a')) {
if ((ch <= 'Z' && ch >= 'A') || (ch <= 'z' && ch >= 'a')) {

if (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')

System.out.println("Second class Sleeper ticket");

else if (tickettype='U')

System.out.println("Unreserved ticket");

}
}

class TicketType {

public static void main(String[] args) {

char tickettype = 'A'; // Initialize the value of tickettype to A, 2, or U

if (tickettype == 'A') {

System.out.println("AC ticket");

} else if (tickettype == '2') {

System.out.println("Second class Sleeper ticket");

} else if (tickettype == 'U') {

System.out.println("Unreserved ticket");

20. Define abstraction

It is a process of hiding all the main details from the user and showing only the functionality to the
user.

21. int res ='A';

What is the value of res?

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

char res= 'A';

output:- 65

22. State the difference between while and do while loop

The while loop is an entry-controlled type of loop.

Least output value is 0

The do-while loop is an exit-controlled type of loop.


Least output value is 1

23. system.out.print ("BEST");

system.out.println("OF LUCK");

Choose the correct option for the output of the above statements

(i) BEST OF LUCK

*(ii) BEST

OF LUCK

*Option no. 2

4. Write the return data type of the following function.

log()

double or float both are correct but I prefer you to write double

5. What is the value of y after evaluating the expression given below?

Y+=++y+y--+--y, when int y=8;

32

5. Give the output of the following

(i) Math.floor (-4.7)

-5

(ii)Math.ceil(3.4)+ Math.pow(2, 3)

12

Convert the following if else if construct into switch case

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");

28. Rewrite the following using ternary operator.

if (bill>10000)

discount =bill *100/100;

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;

for (i=5;i >10; i++)

System.out.println(i);

System.out.println(i*4);

Output will be 0 times

30. Name any two basic principles of Object-oriented Programming.

encapsulation, inheritance, and polymorphism

31. Write a difference between unary and binary operator

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

33. Identify and name the following tokens:

(i) public. =keyword

(II) 'a' =literal

(iii)== =operator

(iv){} =separator

34. Differentiate between if else if and switch-case statements.

if-else if

(i) It evaluates integer, character, pointer or floating-point type or boolean type.

(ii) Which statement will be executed depend upon the output of the expression inside if statement.

switch-case

(i) It evaluates only character or integer value.


(ii) Which statement will be executed is decided by user.

35. Write a Java expression for the following |x2+2xy|

(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;

for (i=5; b=1;i--)

if(i%2==1)

continue;

System.out.print(i+" ");

39. Give the output of the following: Math.sqrt(Math.max(9,16))

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

41. What are the various types of errors in Java?

There are 3 types of Errors:

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.

Part C: Short Questions

1What are the different types of programming approach?

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.

2. What can be called objects?

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

3. Explain the details of a class

A class is a blueprint of objects that share common properties, relationships and behaviours. A class
is defined using the keyword 'class'.

Every Class Consists of

❖ data members (variables)

❖Member functions(methods)

4. Why are classes called Abstract Data types (ADT)?

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).

5. Write three main advantages of the OOP approach

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

1069 model year 1949

7.What is the state and behaviour of an object?

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

8. Explain with What is method overloading? Explain with example.

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.

9. What is a compound statement or a block? Give an example

A compound statement is any number and kind of statements grouped together within curly braces.
For example

for(int i = 0; i < 10; i++)

System.out.println(“aksh good doggy”) //Body of this loop is a compound statement.

10. What is an if statement? Write its syntax

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)

11. What is nested if statement? Explain with the help of flowchart.

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

12. Explain the term loop with an example.

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.

13. Explain different control st-ructures with the help of flowchart

Pg no. 26 and 27.

14. What output does the following code produces?

1st Division

15. What will be the output of the following?

Try to improve yourself //aksh

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 {

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

System.out.print("Enter the number of seconds: ");

int sec = sc.nextInt();

int hrs = seconds / 3600;

int remainingSec = seconds % 3600;

int minutes = remainingSec / 60;

seconds = remainingSec % 60;

System.out.printf("%d seconds = %d hrs, %d mins, %d secs\n", seconds, hours, minutes,


seconds);

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 {

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

System.out.print("Enter a number: ");

int num = sc.nextInt();

if (num > 0) {

System.out.print("The number is positive. ");

if (num % 2 == 0) {

System.out.println("It is even.");

} else {

System.out.println("It is odd.");

} else if (num < 0) {

System.out.println("The number is negative.");

} else {

System.out.println("The number is zero.");

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

For an incorrect option, an appropriate error message should be displayed.


19. Using the switch-case statement, write a menu driven program to do the following

(a) To generate and print Letters from A to Z and their Unicode

Letters Unicode

A 65

B 66

. .

. .

. .

Z 90

(b) Display the following pattern using iteration (looping) statement:

12

1 23

1234

12345

You might also like