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

,,,,

The document discusses an assignment on object-oriented programming concepts for class 9 students. It includes multiple choice and true/false questions about OOP concepts like encapsulation, inheritance, polymorphism etc. It also includes short answer questions about OOP, procedural programming differences and features like abstraction.

Uploaded by

nikkix2412
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)
88 views

,,,,

The document discusses an assignment on object-oriented programming concepts for class 9 students. It includes multiple choice and true/false questions about OOP concepts like encapsulation, inheritance, polymorphism etc. It also includes short answer questions about OOP, procedural programming differences and features like abstraction.

Uploaded by

nikkix2412
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/ 44

SCOTTISH HIGH INTERNATIONAL SCHOOL

CLASS IX (2024-25)
Chapter 1: Introduction to Object Oriented Programming Concepts – Assignment 1

Q1. Multiple Choice Questions

(i). Which of the following is not an OOPS concept?

(a) Encapsulation
(b) Polymorphism
(c) Exception
(d) Abstraction

(ii). Which feature of OOPS described the reusability of code?

(a) Abstraction
(b) Encapsulation
(c) Polymorphism
(d) Inheritance

(iii). Which among the following feature is not in the general definition of OOPS?

(a) Modularity
(b) Efficient Code
(c) Code reusability
(d) Duplicate or Redundant Data

(iv). Which feature of OOPS derives the class from another class?

(a) Inheritance
(b) Data hiding
(c) Encapsulation
(d) Polymorphism

(v). A single program of OOPS contains _______ classes?

(a) Only 1
(b) Only 999
(c) Only 100
(d) Any number

(vi). Which two features of object-oriented programming are the same?

(a) Abstraction and Polymorphism features are the same


(b) Inheritance and Encapsulation features are the same
(c) Encapsulation and Polymorphism features are the same
(d) Encapsulation and Abstraction

(vii). Which of the following definition is incorrect for polymorphism?

(a) Polymorphism helps in redefining the same functionality


(b) Polymorphism concept is the feature of object-oriented programming (OOP)
(c) It always increases the overhead of function definition
(d) Ease in the readability of the program

(viii). Encapsulation is________________?

(a) technique of combining more than one member functions into a single unit.
(b) mechanism of combining more than one data member into a single unit.
(c) mechanism of combining more than one data members and member functions that implement on
those data members into a single unit
(d) technique of combining more than one data members and member functions into a single unit,
which can manipulate any data.

(ix). Which among the following feature does not come under the concept of OOPS?

(a) Data binding


(b) Data hiding
(c) Platform independent
(d) Message passing

(x). Which of the following definition best describes the concept of polymorphism?

(a) It is the ability to process the many messages and data in one way
(b) It is the ability to process the undefined messages or data in at least one way
(c) It is the ability to process the message or data in more than one form
(d) It is the ability to process the message or data in only one form

(xi). Procedure Oriented Programming gives importance to ______________

(a) Instructions only


(b) Instructions and data
(c) Data only
(d) None of these

(xii). Procedure Oriented Programming mainly uses _____________


(a) Top-down approach
(b) Top-down and bottom-up approach
(c) Bottom-up approach
(d) None of these

(xiii). Object Oriented Programming mainly uses __________________

(a) Top-down approach


(b) Top-down and bottom-up approach
(c) Bottom-up approach
(d) None of these

(xiv). An object belonging to a particular class is known as a/an _________________ of that class.

(a) Interface
(b) Instance
(c) Alias
(d) Member

(xv). Objects that share the same attributes and behavior are grouped together into a/an
_________________

(a) Interface
(b) Instance
(c) Alias
(d) Class

(xvi). ______________ refers to the act of representing essential features without including the background
details.

(a) Abstraction
(b) Inheritance
(c) Encapsulation
(d) Polymorphism

(xvii). _________________ is the feature by means of which one class acquires the properties of another
class.

(a) Abstraction
(b) Inheritance
(c) Encapsulation
(d) Polymorphism

(xviii). The ability of a function or object to take on multiple forms is called ________________
(a) Abstraction
(b) Inheritance
(c) Encapsulation
(d) Polymorphism

(xix). The term OOP stands for

(a) Object Oriented Procedure


(b) Object Oriented Packet
(c) Object Oriented Programming
(d) Object Orientation Procedure

Q2. State whether the given statements are True or False

(a) Low-level languages are closer to computer hardware.

(b) In a procedural language, code and data are held separately.

(c) In object-oriented programming, code and data are held separately.

(d) Wrapping up data and related functions in a single unit represents encapsulation.

(e) The object is also known as an instance.

(f) The class that is derived from another class is called a superclass.

(g) Classes can be derived from other classes.

(h) Inheritance allows a class to acquire the properties of another class.

(i) A class is a blueprint for the attributes and behaviors of a group of objects.

(j) Objects from the same class do not share the same definition of attributes and behaviors.
Chapter 1: Introduction to Object Oriented Programming Concepts – Assignment 2

Answer the following questions:


1. What are the two types of programming languages?

2. What are the programming language generations?

3. What do you mean by programming paradigm?

4. What are the different programming paradigms?

5. What is Object Oriented Programming?

6. List the concepts of OOPs

7. Write the differences between Procedural Programming and Object-Oriented Programming.

8. What is an abstraction?

9. What is encapsulation?

10. What is inheritance and how it is useful in Java? Give examples.


Chapter 2: Introduction to Java – Assignment 1

Q1. Multiple Choice Questions

(i). Java applications ___________________

(a) Are platform dependent


(b) Don't need a platform to run
(c) Are platform independent
(d) Can't run on Windows

(ii). JVM stands for ___________________

(a) Java Visual Monitor


(b) Java Video Monitor
(c) Java Virtual Monitor
(d) Java Virtual Machine

(iii). JRE stands for _____________________

(a) Java Runtime Editor


(b) Java Runtime Environment
(c) Java Runtime Expression
(d) Java Runtime Enabler

(iv). Stand-alone Java applications ___________________ for execution.

(a) Need Java-compatible web browser


(b) Need an assembler
(c) Don't need Java-compatible web browser
(d) Need a linker

(v). Java uses_________________ for execution.

(a) Compiler and interpreter


(b) Interpreter only
(c) Compiler only
(d) None of these

(vi). A program that translates code written in a high-level language into machine code is called
____________________

(a) Assembler
(b) Linker
(c) Compiler
(d) None of these

(vii). A program that translates an assembly language program into machine code is called a/an
____________________

(a) Assembler
(b) Linker
(c) Compiler
(d) None of these

(viii). Java is ____________________


(a) Robust
(b) Object oriented
(c) Secure
(d) All of these

(ix). Java can be used to write ___________________

(a) Stand-alone applications only


(b) Both stand-alone and internet applications
(c) Internet applications only
(d) None of these

(x). Java applications can run on __________________

(a) Windows platform


(b) Macintosh platform
(c) UNIX platform
(d) All of these

Q2. State whether given statements are True or False

(a) Stand-alone Java applications cannot run independently on a computer.


(b) A console-based Java application is designed to be used via a text-only interface.
(c) Java Applets are Java applications that run within a web browser.
(d) The machine language version of the source code generated by the compilation process is specific to
the processor you are compiling on.
(e) The Bytecode files are generated with the ".java" extension.
(f) Java uses a combination of compilation and interpretation.
(g) Just-In-Time (JIT) compiler compiles selected portions of Bytecode into executable code.
(h) An interpreter translates the source code instructions into machine code all at once.
(i) JVM stands for Java Virtual Monitor.
(j) Machine language for JVM is called Bytecode.
Chapter 2: Introduction to Java – Assignment 2

1. What was Java originally known as? Who developed Java?


2. What are the characteristics of Java?
3. What is the difference between source code and object code?
4. How does ordinary compilation takes place?
5. Briefly explain the compilation process of Java source code
6. Explain the two types of Java programs.
7. What is a bytecode
8. What do you understand by JVM?
9. What is JDK (Java Development Kit)?
10. What are Java APIs?
11. What do you know about BlueJ?
12. How you create, compile and execute a program in Java or BlueJ? Explain your answer?
13. Why do we use main( )?
Chapter 3: Elementary concepts of Objects and Classes – Assignment 1

Q1. Multiple Choice Questions

(i). An object has ...........

(a) Attributes
(b) State
(c) Behavior
(d) All of these

(ii). A class is _______________

(a) An object factory


(b) A blueprint to create objects
(c) A specification for objects
(d) All of these

(iii). ________________ represents an entity in the real-world with its identity and behavior.

(a) A class
(b) An object
(c) A procedure
(d) A method

(iv). ________________ is a template to create similar objects that share common characteristics and
behavior.

(a) A function
(b) A procedure
(c) An attribute
(d) A class

(v). The values of an object's _____________ represent the state of the object.

(a) Methods
(b) Procedures
(c) Attributes
(d) Classes

(vi). The terms object and ____________________ are often interchangeable.

(a) Instance
(b) Behavior
(c) Attribute
(d) State

Q2. State whether the given statements are True or False

(a) A class is a specification about the object.


(b) Only one instance can be created from a single class.
(c) A class is a user-defined data type.
(d) Real world objects encapsulate state and behavior.
(e) There can be multiple abstraction of the same entity.
(f) Objects interact with each other through messages.
Chapter 3: Elementary concepts of Objects and Classes – Assignment 2
1. Define the terms:
i.Object
ii.Attributes
iii.State
iv.Class
2. How do objects communicate with each other?
3. How can you implement abstraction in programming terms?
4. Classes are user defined type. Explain.
5. How are classes and objects implemented in software terms?
6. “Object is an instance of a class”, Explain
7. State two differences between fundamental and user-defined data type.
8. State two differences between a class and an object.
9. Why is a class called a user-defined data type?
10. What is an access specifier? Which two access specifier is used in a class declaration?
11. Why is a class called an object factory?
12. State two rules you should follow for naming a class.
Chapter 4: Values and Data Types – Assignment 1

Q1. Multiple Choice Questions

(i). ASCII stands for ...........

(a) American Standard Code for Information Interchange


(b) American Simulated Code for Information Interchange
(c) American Standard Code for Interchange of Information
(d) American Standard Code for Interaction of Information

(ii). ASCII is ...........

(a) 6-bit set of codes


(b) 8-bit set of codes
(c) 7-bit set of codes
(d) 16-bit set of codes

(iii). Extended ASCII is ...........

(a) 6-bit set of codes


(b) 8-bit set of codes
(c) 7-bit set of codes
(d) 16-bit set of codes

(iv). Which of the following is not a token?

(a) Keyword
(b) Identifiers
(c) Operators
(d) Procedure

(v). Which of the following is a keyword?

(a) character
(b) break
(c) object
(d) attribute

(vi). Which of the following is not a legal identifier?

(a) _age
(b) $Amount
(c) 9thClass
(d) nullValue

(vii). Which of the following is an invalid integer?

(a) 1999
(b) 1999999
(c) 199 99
(d) 199
(viii). Which of the following is not a character literal?

(a) '\t'
(b) "t"
(c) 't'
(d) All of these

(ix). Which of the following punctuator is the statement terminator in Java?

(a) ;
(b) .
(c) ,
(d) All of these

(x). Which of the following is not a primitive data type in Java?

(a) boolean
(b) short
(c) float
(d) class

(xi). Which of the following is the size of a long data type in Java?

(a) 32 bits
(b) 64 bits
(c) 48 bits
(d) Long data type is not supported in Java.

(xii). Which of the following is the size of a Boolean data type in Java?

(a) 1 bit
(b) 16 bits
(c) 8 bits
(d) Boolean data type doesn't take any space in memory.

Q2. State whether the given statements are True or False

(a) Java supports the use of the ASCII character set only.

(b) The smallest unit in a Java program is known as token.

(c) The Unicode character set uses 8 to 32 bits per character.

(d) In an escape sequence, a character is preceded by a forward slash (/).

(e) In Java, an identifier cannot begin with a $ sign.

(f) The boolean data types are used for storing logical values.

(g) Java offers five types of tokens.

(h) The char data type reserves 8 bits in memory.


(i) If a literal constant contains a decimal point, then it is of the type double by default.

(j) The modifier final can be applied to a variable declaration to ensure that the value stored in the variable
cannot be changed after the variable has been initialized.
Chapter 4: Values and Data Types – Assignment 2

Answer the following Questions:

1. What is Unicode?

2. State the differences between Syntax errors and Logical errors.

3. Differentiate between Testing and Debugging.

4. What are tokens?

5. What are keywords? Can keywords be used as an identifier?

6. What is an identifier? What is the identifier formatting rule of Java? OR What are the rules for naming a
variable?

7. State the difference between Token and Identifier

8. What are literals? How many types of integer literals are available in Java?

9. What is an integer constant? Write integer forming rule of Java

10. What do you mean by Escape sequence and name few escape sequences in Java?

11. What is meant by a floating constant in Java? How many ways can a floating constant be represented into?

12. Differentiate between Integer and Floating type constants.

13. Write the following real constants into fractional form: 0.113E04, 0.417E-04, 0.4E-05, 0.123E02

14. What is a type or „Data Type‟? How this term is related to programming?

15. What is primitive data type? Name its different types.

16. Differentiate the two kinds of data types?

17. How many bytes occupied by the following data types: byte, short, int, long, float, double, char, boolean.

18. What is the range of the following data types: byte, short, int, long, float, double, char, boolean.

19. What do you mean by variables?

20. What do you mean by variables? What do you mean by dynamic initialization of a variable?

21. Differentiate between character and Boolean data type.

22. How can you change a variable to a constant? Give an example. Ans. The keyword final makes a variable
as constant, so that the its value cannot be changed during program execution. e.g final double rate=5.5;
Chapter 5: Operators and Expressions – Assignment 1

Q1. Multiple Choice Questions

(i). An operator taking only single operand for its operation is called ...........

(a) A unary operator


(b) A binary operator
(c) A ternary operator
(d) None of these

(ii). Which one of the following is not a binary operator?

1. AND
2. %
3. ==
4. !

(iii). Which one of the following is not a valid operator in Java?

1. <=
2. !==
3. !=
4. ==

(iv). The statement i = i + 1 is equivalent to ...........

1. i++
2. i += 1
3. ++i
4. All of these

(v). For x = 5, the statement sum = ++x + 8 evaluates to ...........

1. sum = 13
2. sum = 14
3. sum = 15
4. sum = 16

(vi). The statement (1>0) && (1<0) evaluates to ...........

1. 0
2. 1
3. false
4. true

(vii). The statement (1>0) || (1<0) evaluates to ...........

1. 0
2. 1
3. false
4. true
(viii). The statement (1==1)? 1: 0 evaluates to ...........

1. 0
2. 1
3. false
4. true

(ix). The expression 13 % 3 gives the output ...........

1. 4
2. 3
3. 2
4. 1

(x). The expression 13 / 3 gives the output ...........

1. 4
2. 3
3. 0
4. 1

(xi). The statement System.out.println("six " + 3 + 3); gives the output ...........

1. six 33
2. six 6
3. 33 six
4. 6 six

(xii). The expression 4 + 8 % 2 gives the output ...........

1. 6
2. 8
3. 4
4. None of these

Q2. State whether the given statements are True or False

(a) Arithmetic operators + and - also have a unary form.


(b) Operators = and == perform the same operation in Java.
(c) The expression 10 % 4 evaluates to 2.
(d) The expression 3 / 4 evaluates to 0.
(e) The expressions 3 + 4 and "3" + "4" evaluate to the same value.
(f) The expression x = x + 7 is same as x =+ 7.
(g) The new operator allocates memory during runtime.
(h) The statements x = 7 and x == 7 are same.
(i) The expression z =- 7 is same as z = z-7.
(j) The assignment operator (=) is a binary operator.
Chapter 5: Operators and Expressions – Assignment 2

Answer the following Questions:

1. What is the function of an operator? Name the types.

2. What are arithmetic operators?

3. Write major difference between the unary and binary operators?

4. What is increment operator? What are postfix and prefix increment operators?

5. Find the value of x after evaluating x += x++ + –x + 4 where x=3 before the evaluation. Explain your
answer.

6. What do you mean by Relational Operators?

7. What is Logical operators?

8. What do you mean by Assignment Statement or Assignment Operator?

9. Illustrate “?” operator with an example?

10. What is the purpose of new operator?

11. What do you mean by precedence? Illustrate with the help of example

12. What is operands?

13. What do you mean by constant? How you declare a variable as constant variables.

14. What do you mean by type conversion? What is the difference between implicit and explicit type
conversion explain with example.

15. Explain the methods print() and println()?

16. What is an Expression?

17. Mention two different styles of expressing a comment in a program.

18. What is a compound Statement? Give an Example.


Chapter 6 Working with Methods – Assignment 1

Q1. Multiple Choice Questions

(i). A method that does not return a value has a ______________ return type.

(a) Boolean
(b) class
(c) int
(d) void

(ii). A method can return _________________________

(a) Any number of values


(b) 2 values
(c) Only 1 value
(d) 3 values

(iii). If a method returns a value, then it must be ___________________

(a) The same data type as defined in its prototype


(b) void type
(c) int type
(d) Boolean type

(iv). Parameters in the method definition are called __________________

(a) Formal parameters


(b) Actual parameters
(c) Informal parameters
(d) void parameters

(v). The parameters that are passed to the method when it is invoked are called _____________________

(a) Formal parameters


(b) Actual parameters
(c) Informal parameters
(d) void parameters

(vi). The scope of a local variable is limited to the ...........

(a) Windows
(b) Multiple programs
(c) Class
(d) Method or block it is declared in

Q2. State whether the given statements are True or False

(a) A method can return more than one value.


(b) Methods defined as void must return a value.
(c) A method may contain any number of return statements.
(d) The non-static methods need an instance to be called.
(e) The static methods need an instance to be called.
(f) If a method returns a value, then it must be of the same data type as defined in the method prototype.
(g) Parameters in the method definition are called dummy parameters.
(h) Methods reside in a class in Java.
(i) The scope of a local variable is limited to the method or the block it is declared in.
(j) The keyword static makes a variable a class variable.
Chapter 6 Working with Methods – Assignment 2

Answer the following Questions:

(a) What is a method? Explain the various parts of a method.


(b) How do you define and invoke a method?
(c) Explain the role of return statement in a method?
(d) What does void signify in the method prototype?
(e) Explain the difference between actual and formal parameters with one example of each.
(f) Explain static and non-static methods.
(g) Explain the scope of variables in Java
(h) Write a class that contains the following two methods:

1. public static double celsiusToFahrenheit(double celsius)


2. public static double fahrenheitToCelsius(double fahrenheit)

Use the following formulas for temperature conversion:

1. fahrenheit = (9.0 / 5) * celsius + 32


2. celsius = (5.0 / 9) * (fahrenheit - 32)
Chapter 6 Input in Java – Assignment 1

Q1. Multiple Choice Questions

(i). Single line comments can be added using ...........

1. //
2. /* */
3. \\
4. Both A and B

(ii). Which keyword do you use to include a class in your program?

1. import
2. export
3. include
4. impart

(iii). Default delimiter used in the Scanner class is ...........

1. Comma
2. Whitespace
3. Colon
4. There is no default delimiter.

(iv). Which package would you import to display the date and time?

1. java.util
2. java.Date
3. java.io
4. java.lang

(v). Which package would you import for the Scanner class?

1. java.util.*
2. java.awt.*
3. java.io.*
4. java.lang.*

(vi). Errors occur in a program when ...........

1. Syntax of the programming language is not followed.


2. The program does not run properly or does not execute at all.
3. The program produces an incorrect result.
4. All of the above

Q2. Answer the following Questions:

a. What are the different types of variables? How are they initialized?
b. What are the default values of the following primitive types:
i. boolean
ii. int
iii. char
iv. long
v. float
c. What is the default value of reference data type ?
d. What are the different ways to input values in Java?
e. What are the advantages of input through Scanner class?
f. Differentiate between next( ) and nextLine( ).
g. What is the function of a Scanner class? In which package does it belong?
h. Name the functions of the Scanner that is used to:
i. Accept a number of long data type
ii. Accept a group of alphabets.
i. What is a bug? What is debugging?
j. What is the different type of errors that may occur in a Java program?
k. 11.What are syntax errors? Give two examples
l. 12.What are run-time errors? Give two examples.
m. 13.What are logical errors. Give examples.
n. Write the difference between close( ) and exit( ).
Chapter 7: Input in Java – Assignment 1
Q1. Multiple Choice Questions

(i). Single line comments can be added using ...........

1. //
2. /* */
3. \\
4. Both A and B

(ii). Which keyword do you use to include a class in your program?

1. import
2. export
3. include
4. impart

(iii). Default delimiter used in the Scanner class is ...........

1. Comma
2. Whitespace
3. Colon
4. There is no default delimiter.

(iv). Which package would you import to display the date and time?

1. java.util
2. java.Date
3. java.io
4. java.lang

(v). Which package would you import for the Scanner class?

1. java.util.*
2. java.awt.*
3. java.io.*
4. java.lang.*

(vi). Errors occur in a program when ...........

1. Syntax of the programming language is not followed.


2. The program does not run properly or does not execute at all.
3. The program produces an incorrect result.
4. All of the above

Q2. Answer the following Questions:

(a) What is a java package? Give an example.


(b) Explain the use of import statement with an example.

(c) Explain the statement, "a well-documented code is as important as the correctly working code".
(d) How can you write single line comments in Java?
(e) Explain data input technique in a program using the Scanner class.
(f) Distinguish between the following:
i. next() and nextLine()
ii. next() and next().charAt(0)
(g) What are delimiters? Which is the default delimiter used in the Scanner class?
(h) What are errors in a program?
(i) Explain the following terms, giving an example of each.
i. Syntax error
ii. Runtime error
iii. Logical error
( j) A program has compiled successfully without any errors. Does this mean the program is error free?
Explain.
(k) Write a program in Java that uses 'input using initialisation' to calculate the Simple Interest and the
Total Amount with the given values:
i. Principle Amount = Rs. 20000
ii. Rate = 8.9%
iii. Time = 3 years
(l) Write a program in Java that accepts the seconds as input and converts them into the corresponding
number of hours, minutes and seconds. A sample output is shown below:

Enter Total Seconds:


5000
1 Hour(s) 23 Minute(s) 20 Second(s)

(m) Write a program in java to input the temperature in Fahrenheit, convert it into Celsius and display the
value in the Terminal window. A sample output is displayed below:

Enter temperature in Fahrenheit


176
176.0 degree Fahrenheit = 80.0 degree Celsius
Chapter 8: Mathematical Library Methods – Assignment 1
Q1. Multiple Choice Questions
(i). Which Java package includes the Math class?

1. java.io
2. java.lang
3. java.util
4. java.sys

(ii). Give the output of Math.sqrt(x); when x = 9.0

1. 3
2. 3.0
3. 3.00
4. All of these

(iii). Give the output of Math.ceil(46.6).

1. 46.6
2. 46.5
3. 47.0
4. 46.0

(iv). Give the output of Math.abs(x); when x = -9.99

1. -9.99
2. 9.99
3. 0.99
4. None of these

(v). Which of the following is a method to find the square root of a number?

1. FindSquareroot(x)
2. Sqrt(x)
3. Math.Square(x)
4. Math.sqrt(x)

(vi). What will be the output of Math.pow(2, 0)?

1. 0.0
2. 1.0
3. 2.0
4. -1.0

(vii). Math.random() returns a double value r such that

1. 0.0 <= r < 1.0


2. 0.0 <= r <= 1.0
3. 0.0 < r <= 1.0
4. 0.0 < r < 1.0

(viii). Give the output of Math.floor(46.6)?


1. 46.5
2. 47.0
3. -46.0
4. 46.0

Q2. Answer the following questions:

(i). Distinguish between Math.ceil() and Math.floor() methods.


(ii). Explain the following Math functions in Java:

( a ) Math.sqrt()
( b ) Math.cbrt()
( c ) Math.random()
( d ) Math.round()

(iii). Write the following as Java expressions:

( a ) | x3 −y2 −2xy |
( b ) π (z4−2π)
6
( c ) √ (z 2−π )
3

( d ) √ ( x3 – y3 )
4

( e ) amount ∗ rate
1
1- n
(1+rate)

(iv). Write valid statements for the following in Java:

(a) Print the rounded off value of 234.49


(b) Print the absolute value of -9.99
(c) Print the largest of -45 and -50
(d) Print the smallest of -56 and -57.4
(e) Print a random integer between 50 and 70
(f) Print 10.5 raised to the power 3.8

(v). Write a program in Java to find the minimum of three numbers using Math.min() method.
(vi). Write a program to print:

( a ) p to the power q
( b ) the square root of p

(vii). The values of p and q are 64 and 2 respectively.


(viii). Write a program to generate random integers in the range 10 to 20.
(ix). Write the equivalent Java statements for the following, only using the mathematical functions:

( a ) Print the positive value of -101.


( b ) Store the value -125 in a variable and print its cube root.
( c ) Store the value 89.99 in a variable and convert it into its closest integer that is greater than or
equal to 89.99.
Chapter 9: Conditional Constructs in Java – Assignment 1
Multiple Choice Questions
(i). Which operator cannot be used with if-else statement?

(a) <=
(b) ||
(c) &&
(d) ? :

(ii). Predict the output of the following code snippet:

int a = 1;
int b = 2;
if (a == b)
System.out.println ("Both values are equal");
else
System.out.println ("Values are not equal");

(a) Both values are equal


(b) Incorrect use of the == operator
(c) Values are not equal
(d) No output

(iii). Consider the following code snippet:

if ( c > d)
x = c;
else
x = d;

(iv). Choose the correct option if the code mentioned above is rewritten using the ternary operator:

(a) x = (c >d) ? c : d;
(b) x = (c >d) ? d : c;
(c) x = (c >d) ? c : c;
(d) x = (c >d) ? d : d;

if ((a > b) && (a > c)), then which of the following statements is true?

(a) a is the largest number.


(b) b is the largest number.
(c) c is the largest number.
(d) b is the smallest number.

(v). Consider the following code snippet:

int val = 2;
switch (val)
{
case 1: System.out.println("Case 1");
break;
case 2: System.out.println("Case 2");
break;
default: System.out.println("No match found");
break;
}

Which of the following statements is correct?

(a) case 1 will be executed.


(b) case 2 will be executed.
(c) default will be executed.
(d) both case 1 and 2 will be executed.

(vi). A sequence of statements enclosed between a pair of curly brackets is called

1. a compound statements
2. an empty statement
3. a null statement
4. a void statement

(vii). Which clause is optional in the switch statement?

1. default
2. case
3. switch
4. None of these

(viii). Which of the following statements involves a fall-through?

1. if-else
2. for loop
3. if-else-if ladder
4. switch

(ix). Which of the following causes a fall-through in the switch statement?

1. the omission of fall


2. the omission of continue
3. the omission of break
4. the omission of loop

(x). Which of the following is mandatory in the switch statement?

1. break
2. continue
3. case
4. default
Chapter 9: Conditional Constructs in Java – Assignment 2

Answer the following questions


(i). Explain the significance of break statement in the switch statement.
(ii). Explain the significance of the default label in the switch statement.
(iii). Format the following if statements with indentation:

(a) if (x == y) if (x == z) x = 1; else y = 1; else z = 1;


(b) if (x == y) {if (y == z) x = 1; y = 2; } else z = 1;
(c) if (num1 != num2) {
if (num2 >= num3) x = 1; y = 2; }
else {x = 1; if (num1 == num2) z = 3;}

(iv). Rewrite the following if statement, using the switch statement:

if (choice == 0)
System.out.println("You selected Blue");
else if (choice == 1)
System.out.println("You selected Cyan");
else if (choice == 2)
System.out.println("You selected Red");
else if (choice == 3)
System.out.println("You selected Magenta");
else if (choice == 4)
System.out.println("You selected Green");
else if (choice == 5)
System.out.println("You selected Yellow");
else
System.out.println("Invalid choice");

(v). Write the following switch statement by using nested if statements:

switch (choice)
{
case 0:
case 1:
x = 11;
y = 22;
break;
case 2:
x = 33;
y = 44;
break;
default:
y = 55;
break;
}

(vi). Write an if statement to find the smallest of the three given integers using the min() method of the Math
class.
(vii). Using the switch statement in Java, write a program to display the color of the spectrum (VIBGYOR)
according to the user's choice.
(viii). Employees at Arkenstone Consulting earn the basic hourly wage of Rs.500. In addition to this, they
also receive a commission on the sales they generate while tending the counter. The commission given to
them is calculated according to the following table:

Total Sales Commission Rate

Rs. 100 to less than Rs. 1000 1%

Rs. 1000 to less than Rs. 10000 2%

Rs. 10000 to less than Rs. 25000 3%

Rs. 25000 and above 3.5%

Write a program in Java that inputs the number of hours worked and the total sales. Compute the
wages of the employees.

(ix). Write a program in Java to compute the perimeter and area of a triangle, with its three sides given as a,
b, and c using the following formulas:

Perimeter=a + b + c
Area = √ s (s−a)(s−b)(s−c )
Where s= a + b + c
2

(x). Mayur Transport Company charges for parcels as per the following tariff:

Weight Charges

Upto 10 Kg. Rs. 30 per Kg.

For the next 20 Kg. Rs. 20 per Kg.

Above 30 Kg. Rs. 15 per Kg.

Write a program in Java to calculate the charge for a parcel, taking the weight of the parcel as an input.
(xi). The electricity board charges the bill according to the number of units consumed and the rate as given
below:

Units Consumed Rate Per Unit

First 100 units 80 Paisa per unit

Next 200 units Rs. 1 per unit

Above 300 units Rs. 2.50 per unit

Write a program in Java to accept the total units consumed by a customer and calculate the bill. Assume
that a meter rent of Rs. 500 is charged from the customer.

(xii). Write a menu driven program to accept a number from the user and check whether it is a Buzz number
or an Automorphic number.

(a) Automorphic number is a number, whose square's last digit(s) are equal to that number. For
example, 25 is an automorphic number, as its square is 625 and 25 is present as the last two digits.
( b ) Buzz number is a number, that ends with 7 or is divisible by 7.

(xiii). Write a program in Java to accept three numbers and check whether they are Pythagorean Triplet or
not. The program must display the message accordingly. [Hint: h2=p2+b2]

(xiv). A cloth showroom has announced the following festival discounts on the purchase of items based on
the total cost of the items purchased:

Total Cost Discount Rate

Less than Rs. 2000 5%

Rs. 2000 to less than Rs. 5000 25%

Rs. 5000 to less than Rs. 10,000 35%

Rs. 10,000 and above 50%

Write a program to input the total cost and to compute and display the amount to be paid by the
customer availing the discount.
(xv). A box of cookies can hold 24 cookies, and a container can hold 75 boxes of cookies. Write a program
that prompts the user to enter the total number of cookies, the number of cookies in each box, and the
number of cookies boxes in a container. The program then outputs the number of boxes and the number of
containers required to ship the cookies.

(xvi). A special two-digit number is such that when the sum of its digits is added to the product of its digits,
the result is equal to the original two-digit number.
Example: Consider the number 59.
Sum of digits = 5 + 9 = 14
Product of digits = 5 * 9 = 45
Sum of the sum of digits and product of digits = 14 + 45 = 59
Write a program to accept a two-digit number. Add the sum of its digits to the product of its digits. If the
value is equal to the input number, display the message "Special 2 - digit number" otherwise, display the
message "Not a special two-digit number".

(xvii). Using the switch statement, write a menu driven program:

1. To check and display whether a number input by the user is a composite number or not.
A number is said to be composite, if it has one or more than one factors excluding 1 and the number
itself.
Example: 4, 6, 8, 9...
2. To find the smallest digit of an integer that is input:
Sample input: 6524
Sample output: Smallest digit is 2
For an incorrect choice, an appropriate error message should be displayed.
Chapter 10: Iterative Constructs in Java – Assignment 1

Q1. Multiple Choice Questions


(i). Which of the following loop executes at least once?

(a) for
(b) while
(c) do-while
(d) None of these

(ii). Which of the following is an exit-controlled loop?

(a) for
(b) do-while
(c) while
(d) None of these

(iii). Which of the following is an invalid loop?

(a) repeat
(b) while
(c) do-while
(d) for

(iv). Which of the following is not a jump statement in Java?

(a) return
(b) jump
(c) break
(d) continue

(v). How many times will the following code print "Hello"?

for (int i = 1; i <= 5; i++);


{
System.out.println("Hello");
}

(a) 0
(b) 1
(c) 5
(d) 4

(vi). What will be the output of the following code?

public static void main(String args[])


{
int sum = 0;
for (int i= 1; i <= 5; i++)
{
sum = i;
}
System.out.println(sum);
}
(a) 15
(b) 21
(c) 5
(d) 0

(vii). How many times will the following loop execute?

public static void main(String args[])


{
int sum = 0;
for (int i = 10; i > 5; i++)
{
sum += i;
}
System.out.println(sum);
}

(a) 5
(b) 0
(c) 15
(d) Infinite loop

(viii). How many times will the following loop execute?

public static void main(String args[])


{
int i = 1;
while (i < 10)
if (i++ % 2 == 0)
System.out.println(i);
}

(a) 4
(b) 5
(c) 0
(d) 10

Q2. State whether the given statements are True or False

(i). To execute a do-while loop, the condition must be true in the beginning.
(ii). The while loop is an exit-controlled loop.
(iii). The while part of a do-while statement must be terminated by a semicolon.
(iv). The continue statement terminates the current loop and then continues from the statement
immediately following the current loop.
(v). The return statement is a jump statement.
(vi). The for loop may contain multiple initializations and updates.
(vii). A loop that never terminates is called an empty loop.
(viii). The do-while loop executes at least once even if the condition is false.
Chapter 10: Iterative Constructs in Java – Assignment 2
Answer the following Questions

1. What are iteration statements? Name the iteration statements provided by Java?
2. Name the Elements that Control a Loop.
3. What is the difference between entry controlled and exit controlled loop? or What is the difference
between while and do-while loop?
4. 4. Explain the difference between break and continue with an example.
5. Compare and discuss the suitability of three loops in different situation?
6. Explain the term for loop with an example.
7. Give an example to show how multiple initializations and updations are performed in a for-loop.
8. What do you mean by an infinite loop? Give an example.
9. Differentiate Null loop and Infinite loop.
10. What do you mean by delay loop?
11. Compare loops according to its usage.
12. Give the general syntax of a do-while loop. How do you create infinite loops using do-while loop
structure?
13. Give the general syntax of a while-loop. How do you create infinite loops using a while loop structure?
14. State one similarity and one difference between while and do-while loop.
15. Name two jump statements and their use.
Chapter 11: Nested for Loops – Assignment 1

Q1. Fill in the blanks


(i). A loop within another loop is called _______________________
(ii).You can use _______________________statement to terminate a loop block.
(iii). _______________________is a tag that decides which of part the loop is to be used.
(iv). Repetition of ________________loop takes place before _______________loop
(v)._______________________statement will repeat a loop for next iteration after ignoring some
statements of the loop.

Q2. Write whether the following statements are True/False


(i). Loop is a repetitive structure.
(ii).Nesting of loops means restricting the use of inner loop.
(iii). When break statement is applied, it terminates the program completely.
(iv). When outer loop completes its iterations, the inner loop starts.
(v).Labelled continue statement allows the next iteration of the loop from any place of looping structure.

Q3. Differentiate between the following:


(i). Nested if and nested loop
(ii).Break and continue
(iii). Labelled break and Unlabeled break

Q4. Answer the following questions


(i). What do you mean by a nested loop?
(ii).In what situation you need a nested loop?
(iii). How will you terminate outer loop from the block of the inner loop?
(iv). What do you mean by labelled break statement? Give an example.
(v).Write down the syntax of the nested loop.

Q5. Give the output of the following snippets based on nested loops
(i). int i,j;
for (i=0; i<4; i++)
{
for (j=i; j>=0; j--)
System.out.print(j);
System.out.println();
}

(ii).int y,p;
for (int x=1; x<=3; x++)
{
for (y=1; y<=2; y++)
{
p = x * y;
System.out.print(p);
}
System.out.println( );
}

(iii). int a,b;


for (a=1; a<=2; a++)
{
for (b= (64+a); b<=70; b++)
System.out.print((char) b);
System.out.println( );
}

(iv). int x,y;


for(x=1; x<=5; x++)
{
for(y=1; y<x; y++)
{
if(x == 4)
break;
System.out.print(y);
}
System.out.println( );
}

(v). int i,j;


first:
for (i=10; i>=5; i--)
{
for (j= 5; j<=i; j++)
{
if (i*j <40)
continue first;
System.out.print(j);
}
System.out.println( );
}
Chapter 11: Nested for Loops – Assignment 2

Q1. Multiple Choice Questions

(i). loop within another loop statement is called


( a ) Inner structure
( b ) Nested loop
( c ) Complex loop
( d ) Infinite loop

(ii).Which of the following loop can be used as outer loop ?


( a ) while
( b ) do while
( c ) for
( d ) All of these

(iii). Nested loop structure is used for.


( a ) Multi dimensional arrays
( b ) Matrix manipulation
( c ) Complex Pattern
( d ) All of These

(iv). Examine the following code. How many times will the nested loop run?

(a) 3
(b) 6
(c) 1
(d) 9

(v).How often is the inner loop of a nested loop run?


( a ) Forever
( b ) One fewer time than the main loop
( c ) Each time the main loop is run
( d ) One more time than the main loop

(vi). Examine the following code. Is there anything that will prevent it from processing?

(a) No, it is correct


(b) No, but it could run forever
(c) Yes, it will return a compiler error
(d) Yes, but it will show all zeroes in the output
(vii). Which loop construct in Java is best suited when the number of iterations is known?
( a ) for loop
( b ) while loop
( c ) do-while loop
( d ) break statement

(viii). What is the purpose of the continue statement in a loop?


(a) To exit the loop immediately
(b) To skip the current iteration and move to the next iteration
(c) To terminate the program
(d) To execute a specific block of code

(ix). Which loop construct in Java is best suited when the number of iterations is unknown?
( a ) for loop
( b ) while loop
( c ) do-while loop
( d ) none

(x).What is the key difference between a while loop and a do-while loop in Java?
( a ) The syntax used to define the loop
( b ) The number of iterations performed
( c ) The condition check timing
( d ) The ability to use the break statement

(xi). Which loop construct guarantees that the loop body is executed at least once?
( a ) for loop
( b ) while loop
( c ) do-while loop
( d ) continue statement

(xii). What is an infinite loop?


( a ) A loop that executes only once
( b ) A loop that never terminates naturally
( c ) A loop that contains an unreachable code block
( d ) A loop that uses the continue statement

(xiii). Which statement is used to exit a loop prematurely?


(a) return statement
(b) continue statement
(c) break statement
(d) exit statement

(xiv).Which loop construct is best suited for iterating over an array or a collection?
( a ) for loop
( b ) while loop
( c ) do-while loop
( d ) continue statement

(xv). Which type of loop is best known for its boolean condition that controls entry to the loop?
( a ) do-while loop
( b ) for (traditional)
( c ) for-each
( d ) while

(xvi).Which type of loop is best known for using an index or counter?


( a ) do-while loop
( b ) for (traditional)
( c ) for-each
( d ) while

(xvii). Which of the following can loop through an array without referring to the elements by index?
( a ) do-while loop
( b ) for (traditional)
( c ) for-each
( d ) while

(xviii). What is the output of the following code snippet?


int i = 0;
for(i = 0 ; i < 5; i++){
}
System.out.println(i);
(a) 5
(b) 0
(c) 4
(d) Compilation Error

(xix).What is the output of the following program?


public class Test{
public static void main(String []args){
int i = 0;
for(i = 0; i < 10; i++){
break;
}
System.out.println(i);
}
}
(a) 1
(b) 0
(c) 10
(d) 9

(xx). What is the output of the following program?


public class Test{

public static void main(String []args){


int i = 0;
for(i = 0; i < 10; i++){
continue;
}
System.out.println(i);
}
}
(a) 10
(b) 0
(c) Compilation error
(d) 9

(xxi).What is the output of the following program?


public class Test{
public static void main(String []args){
for(int i = 0; i < 10; i++){
if(i % 2 == 0){
continue;
}
System.out.println(i);
}
}
}
(a) Program will print all even numbers between 0 to 10
(b) The program will print all odd numbers between 0 to 10
(c) Program gives a compilation error
(d) None of the above
Q2. Write the Java code for the following programs:
( i ) . Write a program to input a number and perform the following tasks:
(a) to check whether it is a prime number or not
(b) to reverse the number

If the number as well as the reverse is also 'Prime' then display 'Twisted Prime' otherwise 'Not a twisted
Prime'.
Sample Input: 167
Sample Output: 167 and 761 both are prime.
It is a 'Twisted Prime'.

( i i ) . Write programs to find the sum of the given series:


1 + (1/2!) + (1/3!) + (1/4!) + .......... + (1/n!)
1 + (1+2) + (1+2+3) + .......... + (1+2+3+ ...... + n)
1 + (1*2) + (1*2*3) + .......... + (1*2*3* ...... * n)
1 + 1 / (1+2) + 1 / (1+2+3) + .......... + 1 / (1+2+3+.....+n)
(1/2) + (1/3) + (1/5) + (1/7) + (1/11) + .......... + (1/29)

( i i i ) . Write the programs in Java to display the following patterns:


(a)
1
21
321
4321
54321

(b)
12345
1234
123
12
1

(c)
54321
5432
543
54
5

(d)
13579
1357
135
13
1

(e)
5
54
543
5432
54321

(f)
12345
2345
345
45
5

(g)
99999
77777
55555
33333
11111

(h)
9
79
579
3579
13579

(i)

9
97
975
9753
97531

(j)

1
23
456
7 8 9 10
11 12 13 14 15

You might also like