Chapter 1 – Unit 1
I. Fill in the blanks :
1. An act of using essential features without including background details is called _________________
2. In an Object Oriented Programming, the stress is given on _______________
3. Wrapping of data and function together as a single unit is called ________________
4. An ________________has unique identity through which it may differ with some characteristics and behaviour.
5. The objects may communicate with each other through _________________
6. In POP, the global data are loosely attached to the ________________
7. The process by which a class acquires the property of another class is known as _________________
8. In Object Oriented Programming, using a function for many purposes is termed as ________________
Ans.
1. data abstraction 2. data items 3. encapsulation
4. object 5. Functions 6. functions
7. inheritance 8. polymorphism
II. State whether the given statements are TRUE or FALSE
1. A process according to which a class acquires the characteristics from another class is Encapsulation.
2. Procedure oriented program stresses on data.
3. C++ is also an object oriented programming language.
4. An object is identified by its characteristics.
5. Function is a set of objects that share the common state and behaviour.
6. Encapsulation keeps data safe from outside interference.
Ans.
1. false. [The process mentioned is inheritance]
2. false [Procedure oriented program stresses on functions.]
3. true
4. true
5. true
6. true
III. Answer the following questions:
1. What is meant by Object Oriented Programming?
Ans.
Object Oriented Programming (OOP) is a modular approach which allows the data to be applied within a stipulated program area. The main aim of OOP is to bind data and functions together so that no other part of the code can access this data except that function.
2.What are the elements of Object-Oriented Programming?
Ans. The elements of Object-Oriented Programming are classes, objects and the principles of OOP like Abstraction, Encapsulation, Inheritance and Polymorphism.
3. List the features of Object-Oriented Programming.
Ans. i)The focus is on the data items rather than functions.
ii) It makes the complete program simpler by dividing it into a number of objects.
iii) The objects can be used as a bridge to have data flow from one function to another.
iv) The concept of data hiding enhances security in programs.
v) It is highly beneficial to solve complex problems.
Thus, the essential features of OOP can be summarized under the headers like Data Abstraction, Encapsulation, Data Hiding, Inheritance, Polymorphism and Dynamic Binding.
4. In what way is Object Oriented Programming different from Procedure Oriented Programming.
Ans. Procedure Oriented Programming
i) The stress is on the function rather than the data.
ii) It allows the data to flow freely throughout the program
iii) It follows top-down programming approach.
Object Oriented Programming
i) The stress is put on data rather than functions.
ii) The data is restricted to be used in a specific program area.
iii) It follows bottom-up programming approach.
5. Name any two Object Oriented Programming principles.
Ans. The principles of OOP are:
i)Data abstraction
ii) Encapsulation
iii) Data Hiding
iv) Inheritance
v) Polymorphism
vi) Dynamic Binding.
[Note: write any two of the above]
6. Name any two Object Oriented Programming and Procedure Oriented Programming languages.
Ans. OOP: C++, Java, Python, Smalltalk, Ruby, Eiffel
POP: BASIC, COBOL, FORTRAN, C
[Note: write any two of the above]
7. Which OOP principle implements function overloading?
Ans. Polymorphism. Polymorphism is the process of using a function or method for more than one purpose. This is achieved by creating many methods or functions with the same name but different parameters.
8. Define the following terms:
a) Encapsulation with an example
b) Data abstraction with an example.
Ans. a) Encapsulation is the wrapping of data and functions of a class into a single unit. Moreover, the data is kept hidden and cannot be accessed directly outside the class although it is available in the program. This can be achieved by making all the data members of a class private.
9. Explain inheritance with a suitable example.
Ans.
Inheritance is an OOP principle according to which a class acquires some features from another class. It promotes a characteristic called reusability. For example, consider a class ‘Vehicle’ and another class ‘Car. Assuming that the ‘Vehicle’ class is already available, the ‘Car’ class can created by reusing many of the features of the ‘Vehicle’ class.
Base class – The existing class whose properties are inherited by another class is called as the Base class/ Super class/Parent class.
Derived class – The class that acquires the properties from an existing class is known as Derived class/ Subclass/ Child class.
In the above example, the ‘Vehicle’ class is the Base class and the ‘Car’ class is the Derived class.
10. What is meant by Polymorphism? Explain with an example.
Ans.
Polymorphism means ‘many forms. It is the process of using a function or method for many purposes. Polymorphism allows the class to have multiple functions with the same name but different parameters (Function Overloading). When a function call is encountered, the system matches the number and data type of the parameters passed with the function definitions and that particular function is executed.
For example, the function name Volume( ) can be used by different functions to calculate the volume of a cube or the volume of a cuboid etc as follows:
Volume(int s)
{
}
Volume(int l, int b, int h)
{
}
*********
Chapter 1 – Unit 2
I. Write True or False :
1. Java application is a Java program which is developed by users.
2. James Gosling developed Java programming language.
3. Machine codes are expressed using alphanumeric characters.
4. Bytecode is the program in binary form.
5. JVM is Java Visual Management.
Ans.
1. false 2. True 3. false
4. true 5. false
II. Fill in the blanks with appropriate words :
1. Java is a __________________sensitive language.
2. In Java, the package used to find power raised to any base is ___________
3. The words which are preserved with the system are called _____________ words, that cannot be used as variable names
in Java programming.
4. A single line comment is represented by the symbol _______________in Java programming.
5. BlueJ is a ________________based platform to operate Java program.
Ans.
1. case sensitive 2. java.math 3. key or reserved
4. // 5. windows
III. Answer the following questions:
1. Who developed Java? What was it initially called?
Ans.
Java is an Object-Oriented Programming language developed primarily by James Gosling and his colleagues at Sun Micro Systems. The language was initially called ‘OAK’ (named after the Oak trees outside Gosling’s office)
2. Give a brief historical development of Java.
Ans.
In 1991, Sun Micro Systems developed a complete language as a part of a research work to develop software for consumer electronics. It was developed as a full-fledged programming language like BASIC and C++. Java’s platform independence is one of the most significant advantages that Java had over other languages. Originally, Java was designed to execute applets in a web browser. But gradually, the language has gained wide acceptance as a programming language, replacing C and C++.
3. Mention at least four features of Java.
Ans.
Java possess the following main features:
- Java is an Object-Oriented Programming language.
- Java programs were both compiled and interpreted.
- Java can access data from the local system as well as from the net.
- Java doesn’t require the inclusion of any pre-processor (#) or header files like C++.
4. Define the following:
a) A compiler b) An interpreter c) Byte code
Ans.
All high-level languages need to be converted to machine code (binary code) so that the computer can understand the program after taking the required inputs.
a) A compiler:
During compilation, a compiler converts the code written in high-level language (source code) into machine language at once and displays all the errors together. The program after compilation is called as the Object Code. A compiler is also called as a translating program or system program since it translates the instructions of a high-level language to machine language.
Note:
- Each high-level language requires a separate compiler for conversion.
- A computer system may have more than one compiler for more than one high-level language.
b) An interpreter:
The software which converts the high-level instructions line by line to machine level language is known as interpreter. If an error is found on any line, the execution stops till it is corrected. This process of correcting errors is easier but the program takes a long time to execute successfully. An interpreter is generally used in micro-computers. It helps the programmer to find out the errors and correct them before control moves to the next statement.
Note: Both compilers and interpreters are system software which are also known as language processors.
c) Byte code:
Java language treats compiler and interpreter in different ways. The Java compiler is a software that converts source code into intermediate binary form called byte code. Further, the Java interpreter (also called as the Java Virtual Machine) accepts the byte code and converts it into machine code suitable for the specific platform. Thus, the byte code is platform independent which means that it works irrespective of the machine on which it is to be executed.
5. What is Java Virtual Machine (JVM)?
Ans.
The byte code generated by the compiler needs to be modulated according to the machine on which it is to be executed. The Java interpreter converts the byte code into machine code. Although a software, the Java Interpreter is called as the Java Virtual Machine (JVM) since it acts as a machine to produce object code which is compatible to the specific machine.
6. Name three packages of Java Class Library.
Ans.
- java.applet – To support classes to generate applet specific environment.
- java.awt – To support abstract window tool kit and managing GUI (Graphic User Interface)
- java.lang – To support classes containing String/Character, Math, Integer, Thread etc
7. What are reserved words? Name any five.
Ans.
Java reserved words or the keywords are those words which carry special meaning to the system compiler. These words are basically used for writing a Java statement in the program. Such words cannot be used for naming a variable in the program. Some of the keywords in Java are:
- private
- switch
- default
- static
- void
8. Distinguish between
a) Source code and Object code
Ans.
Source code: It is the code written by the programmer in a High-Level programming language.
Object code: During compilation, the source code (program in high level language) is converted into machine language. This code that is generated after compilation is known as Object Code.
b) Compiler and Interpreter
Ans.
Compiler:
i) It converts the whole source program into object program at once.
ii) It displays the errors for the whole program together after compilation.
Interpreter:
i) It converts the source program into object program one line at a time.
ii) It displays the errors of one line at a time and after correction the control goes to the next line.
c) JDK 1.3 and BlueJ
Ans.
JDK1.3 (DOS based JDK 1.3):
i) Compilation and execution of DOS based JDK 1.3 is not very simple and menu based.
ii) Debugging is not very simple as the code is in one window (e.g. notepad) and the errors are listed in the other window (DOS prompt).
iii) A sample program is not available for the user each time a class is declared.
BlueJ:
i) Since it is a windows-based platform for JDK 1.3. and menu based, it is relatively easier to use.
ii) Debugging can be done easily as it indicates the errors at the bottom of the same window. The task can be performed more conveniently.
iii) A sample program is available each time as soon as a class is declared. This program gives a brief idea of coding in Java.
9. A compiler is specific to a language. Give your comments.
Ans. Yes compilers are language specific. A compiler is a typical program that converts the statements in a high-level language to machine level language. Hence compiler will be language specific since it needs to understand corresponding language to convert into machine language.
10. What is the basic format of a Java program? Explain with an example.
Ans.
A java program usually contains the following sections
- Documentation Section (Comments): [optional]
Comments are non-executable statement in Java. It is included so that it is easier for the programmer to understand or debug the coded. Comments may be of three types single line or multi-line or documentation comments.
- Import statement: [optional]
Many predefined classes are stored in packages in Java. The import statement is used to import a single class or all the classes in a Java package.
- Class declaration: A class is a collection of variables and methods. Every program in Java will have at least one class with the main method. Class declaration starts with the access specifier (public / private /protected) followed by the keyword ‘class’ and then the class name.
Syntax
<access specifier> class <class name>
{
}
- Declaration of main function: Usually the execution of the Java program starts from the main function. The main( )function encloses the programming statements within curly braces. The main function is further enclosed within the class heading.
Syntax
<access specifier> <return type> <method name> ( <data type> <parameter>)
{
}
- Example
//program to add two numbers
public class Sample
{
public static void main(String args[])
{
int a=10,b=15,c=0;
c=a+b;
System.out.println(“Sum=”+c);
}
}
11. What is BlueJ?
Ans.
BlueJ is a free Java environment from Monash University, Australia. It is a windows-based platform for Java Development Kit (JDK). JDK 1.3 must be installed before installing BlueJ. It can be downloaded from the website www.bluej.org free of cost.
12. Mention five features of BlueJ.
Ans.
i) Since it is a windows-based platform for JDK 1.3. and menu based, it is relatively easier to use.
ii) The compilation as well as the execution of the program is comparatively easier than the DOS based JDK 1.3 platform.
iii) Debugging can be done easily as it indicates the errors at the bottom of the same window.
iv) A sample program is available each time as soon as a class is declared. This program gives a brief idea of coding in Java.
v) It also supports the syntax of the program which is written in JDK 1.3 (DOS based)
13. Name a package that is invoked by default.
Ans.
‘java.lang’ is the default package of Java programming (JDK). It will be imported automatically in any Java program to avail its classes and functions.
14. What are the points to be taken care while naming a class in a Java program?
Ans.
- Class name must not be a reserved word.
- A class name cannot have space in-between.
- Special characters other than $(dollar) sign and _ (underscore) cannot be used.
- A class name cannot start with a number.
- As far as possible, class names should be meaningful and relevant to the program.
15. Java is case sensitive. Explain.
Ans.
Java is case sensitive. This means that the upper case and lower case letters are distinguished by the language. For example in the declaration, int y , Y; Both the variables ‘y’ and ‘Y’ are considered different since they are in different case. Likewise, keywords typed in the wrong case (for e.g. VOID) generates syntax errors due to case sensitivity.
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?
Ans.
public – It is an access specifier used in Java to declare that a member’s access is public.
static – When a variable or a method is static, it means that it belongs to the class rather than a specific instance.
void – It is a keyword used to indicate that the method does not return any value.
main – This method is usually the starting point from where the compiler starts program execution. So the compiler needs to call the main() method.
17. What does the word ‘Compilation’ mean?
Ans.
Compilation is the process during which a compiler converts the source program in high-level language into object program or equivalent machine language at once and displays all the errors together. The compiler can only identify and list the syntax errors in the program but not logical errors.
18. Java program uses Compiler as well as Interpreter. Explain.
Ans.
- Compiler and Interpreter are used in various computer languages. Each computer language uses either the Compiler or the Interpreter to convert the source code into object code.
- In Java, the Compiler (software) converts the source code into an intermediate binary code called ‘byte code’. The generated byte code is platform independent and can be executed on any machine.
- The Java Interpreter accepts the byte code and converts it into machine code suitable to a particular platform.
19. Design a program in Java to display the following information on the output screen:
Name:
Class:
Roll No:
Subject:
School:
Ans.
class Q19
{
static void main(String n, String class, int r_no, String sub, String sch )
{
System.out.println(“Name :”+n);
System.out.println(“Class:”+class);
System.out.println(“Roll No :”+r_no);
System.out.println(“Subject : +sub);
System.out.println(“School :”+sch);
}
}
20. You want to display your bio-data on the output screen. Write a program in Java to perform the task in the given format:
Name:
Father’s Name:
Date of birth :
Blood Group:
Aadhar Card No.:
State:
Ans.
class Q20
{
static void main(String n, String fname, String dob, String b_gp, String adhar, String STATE )
{
System.out.println(“Name :”+n);
System.out.println(“Father’s Name:”+fname);
System.out.println(“Date of Birth:”+dob);
System.out.println(“Blood Group : +b_gp);
System.out.println(“Aadhar Card No:”+adhar);
System.out.println(“State :”+STATE);
}
}
*************