0% found this document useful (0 votes)
36 views69 pages

Oop I

Uploaded by

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

Oop I

Uploaded by

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

S. S.

Agrawal Institute of Engineering &


Technology, Navsari

Computer Engineering Department

Subject Name with Code

Object Oriented Programming -I


[3140705]

Study Materials

B.E. – 2nd C.E., 4th Semester


Index

Sr.
Content Page No.
No

1. Syllabus Copy

2. List of Practical

3. List of Assignment

4. Last Five-Year Question Papers

5. Question bank

6. Frequently asked questions with answer

7. Reference Book
Syllabus Copy
GUJARAT TECHNOLOGICAL UNIVERSITY
Bachelor of Engineering
Subject Code: 3140705
Semester – IV
Subject Name: Object Oriented Programming -I
Type of course: core course

Prerequisite: None

Rationale: Object oriented Programming has become a fundamental part of software development. OOP
facilitates Reuse of code, flexibility, effective problem solving. It provides a modular structure for programs
and implementation details are hidden. Reuse of code lowers the cost of development.

Teaching and Examination Scheme:

Teaching Scheme Credits Examination Marks Total


L T P C Theory Marks Practical Marks Marks
ESE (E) PA (M) ESE (V) PA (I)
4 0 2 5 70 30 30 20 150

Content:

Sr. No. Content Total


Hrs

1 Introduction to java and elementary programming: 4


Java language specification API, JDK and IDE, Creating, compiling and Executing a
simple java program, Programming style, documentation and errors, Reading input from
console, identifiers and variables, Assignment statements, Named constants and naming
conventions, Data Types (Numeric, Boolean, Character, String) its Operations and
Literals, Evaluating Expressions and operator Precedence, Types of Operators (Augmented
assignment, Increment and Decrement, Logical), operator precedence and associativity,
numeric type conversions.
2 Selections , Mathematical functions and loops: 4
If statements, Two way, Nested if and multi-way if statements, Switch statements,
Conditional Expressions, Common mathematical functions ,While , do-while and for loop,
nested loops, Keyword break and continue.
3 Methods and Arrays: 6
Defining and calling method, Passing argument by values, Overloading methods and scope
of variables, Method abstraction and stepwise refinement, Single Dimensional arrays,
copying arrays ,Passing and returning array from method, Searching and sorting arrays
and the Array class, Two-Dimensional array and its processing, Passing Two-dimensional
Array to methods, Multidimensional Arrays.
4 Objects and Classes: 4
Defining classes for objects, Constructors, accessing objects via reference variable, using
classes from the java library, static variables, constants and methods, visibility modifiers
and Data field encapsulation, passing objects to methods, array of objects, immutable
Page 1 of 5

w.e.f. AY 2018-19
GUJARAT TECHNOLOGICAL UNIVERSITY
Bachelor of Engineering
Subject Code: 3140705
objects and classes, scope of variable and the this reference.

5 Object oriented thinking: 5


Class abstraction and Encapsulation, thinking in objects and class relationships, Primitive
data type and wrapper class types, Big integer and Big decimal class, string class, String
Builder and String Buffer class, super class and subclass, using super keyword, overriding
and overloading methods, polymorphism and dynamic binding, casting objects and
instanceof operator, The ArrayList class and its methods, The protected data and methods.
6 Exception Handling, I/O, abstract classes and interfaces: 4
Exception types, finally clause, rethrowing Exceptions, chained exceptions, defining
custom exception classes, file class and its input and output, Reading data from web,
Abstract classes, interfaces, Comparable and Cloneabal interface.
7 JAVAFX basics and Event-driven programming and animations: 5
Basic structure of JAVAFX program, Panes, UI control and shapes, Property binding, the
Color and the Font class, the Image and Image-View class, layout panes and shapes,
Events and Events sources, Registering Handlers and Handling Events, Inner classes,
anonymous inner class handlers, mouse and key events, listeners for observable objects,
animation
8 JAVAFX UI controls and multimedia: 4
Labeled and Label, button, Checkbox, RadioButton, Textfield, TextArea, Combo Box,
ListView, Scrollbar, Slider, Video and Audio.
9 Binary I/O ,Recursion and Generics: 4
Text I/O, binary I/O, Binary I/O classes, Object I/o, Random Access files, Problem solving
using Recursion, Recursive Helper methods, Tail Recursion, Defining Generic classes and
interfaces, Generic methods, Raw types and backward compatibility, wildcard Generic
types, Erasure and Restrictions on Generics.
10 List, Stacks, Queues and Priority Queues: 4
Collection, Iterators, Lists, The Comparator interface, static methods for list and
collections, Vector and Stack classes, Queues and priority Queues.
11 Sets and Maps: 2
Comparing the performance of Sets and Lists, singleton and unmodifiable collections and
Maps.
12 Concurrency 2
Thread states and life cycle,Creating and Executing threads with the Executor
Framework, Thread synchronization

Suggested Specification table with Marks (Theory): (For BE only)

Distribution of Theory Marks

R Level U Level A Level N Level E Level C Level


10 50 10 - - -

Legends: R: Remembrance; U: Understanding; A: Application, N: Analyze and E: Evaluate C:


Create and above Levels (Revised Bloom’s Taxonomy)

Page 2 of 5

w.e.f. AY 2018-19
GUJARAT TECHNOLOGICAL UNIVERSITY
Bachelor of Engineering
Subject Code: 3140705

Note: This specification table shall be treated as a general guideline for students and teachers. The actual
distribution of marks in the question paper may vary slightly from above table.

Reference Books:

1) Intro to Java Programming, 10th edition, Y.Daniel Liang, Pearson

2) Object oriented programming with Java , Rajkumar Buyya,S Thamarai Selvi, Xingchen Chu,
McGrawHill

3) Programming in Java, Sachin Malhotra, Saurabh Choudhary, Oxford

4) Programming with JAVA , E Balagurusamy, McGrawHill

5) CORE JAVA volume -I Cay Horstmann, Pearson

Course Outcomes:

Sr. CO statement Marks % weightage


No.

CO-1 Use various Java constructs, features and libraries for simple problems. 20

CO-2 Demonstrate how to define and use classes, interfaces, create objects and 20
methods, how to override and overload methods, compile and execute
programs.

CO-3 Write a program using exception handling, multithreading with 20


synchronization.

CO-4 Write a program using Files, binary I/O, collection Frameworks for a 30
given problem.

CO-5 Design and develop GUI based applications in a group using modern 10
tools and frameworks.

List of Experiments:

(1) Write a Program that displays Welcome to Java, Learning Java Now and Programming is fun.

Page 3 of 5

w.e.f. AY 2018-19
GUJARAT TECHNOLOGICAL UNIVERSITY
Bachelor of Engineering
Subject Code: 3140705
Write a program that solves the following equation and displays the value x and y:
(2) 1) 3.4x+50.2y=44.5 2) 2.1x+.55y=5.9 (Assume Cramer’s rule to solve equation
ax+by=e x=ed-bf/ad-bc
cx+dy=f y=af-ec/ad-bc )
(3) Write a program that reads a number in meters, converts it to feet, and displays the result.

Body Mass Index (BMI) is a measure of health on weight. It can be calculated by taking your
(4) weight in kilograms and dividing by the square of your height in meters. Write a program that
prompts the user to enter a weight in pounds and height in inches and displays the BMI.
Note:- 1 pound=.45359237 Kg and 1 inch=.0254 meters.
(5) Write a program that prompts the user to enter three integers and display the integers in decreasing
order.
(6) Write a program that prompts the user to enter a letter and check whether a letter is a vowel or
constant.
(7) Assume a vehicle plate number consists of three uppercase letters followed by four digits. Write a
program to generate a plate number.
(8) Write a program that reads an integer and displays all its smallest factors in increasing order. For
example if input number is 120, the output should be as follows:2,2,2,3,5.
Write a method with following method header.
(9)
public static int gcd(int num1, int num2)
Write a program that prompts the user to enter two integers and compute the gcd of two integers.
(10) Write a test program that prompts the user to enter ten numbers, invoke a method to reverse the
numbers, display the numbers.
(11) Write a program that generate 6*6 two-dimensional matrix, filled with 0’s and 1’s , display the
matrix, check every raw and column have an odd number’s of 1’s.
(12) Write a program that creates a Random object with seed 1000 and displays the first 100 random
integers between 1 and 49 using the NextInt (49) method.
Write a program for calculator to accept an expression as a string in which the operands and
(13)
operator are separated by zero or more spaces.
For ex: 3+4 and 3 + 4 are acceptable expressions.
Write a program that creates an Array List and adds a Loan object , a Date object , a string, and a
(14)
Circle object to the list, and use a loop to display all elements in the list by invoking the object’s to
String() method.
Write the bin2Dec (string binary String) method to convert a binary string into a decimal number.
(15)
Implement the bin2Dec method to throw a NumberFormatException if the string is not a binary
string.
Write a program that prompts the user to enter a decimal number and displays the number in a
(16) fraction.
Hint: Read the decimal number as a string, extract the integer part and fractional part from the
string.
(17) Write a program that displays a tic-tac-toe board. A cell may be X, O, or empty. What to display at
each cell is randomly decided. The X and O are images in the files X.gif and O.gif.
(18) Write a program that moves a circle up, down, left or right using arrow keys.

(19) Write a program that displays the color of a circle as red when the mouse button is pressed and as
blue when the mouse button is released.
Page 4 of 5

w.e.f. AY 2018-19
GUJARAT TECHNOLOGICAL UNIVERSITY
Bachelor of Engineering
Subject Code: 3140705
(20) Write a GUI program that use button to move the message to the left and right and use the radio
button to change the color for the message displayed.
Write a program to create a file name 123.txt, if it does not exist. Append a new data to it if it
(21)
already exist. write 150 integers created randomly into the file using Text I/O. Integers are
separated by space.
(22) Write a recursive method that returns the smallest integer in an array. Write a test program that
prompts the user to enter an integer and display its product.
(23) Write a generic method that returns the minimum elements in a two dimensional array.

(24) Define MYPriorityQueue class that extends Priority Queue to implement the Cloneable interface
and implement the clone() method to clone a priority queue.
(25) Write a program that reads words from a text file and displays all the nonduplicate words in
descending order.The text file is passed as a command-line argument.

Major Equipment:

Computer, Laptop

List of Open Source Software/learning website:

https://www.tutorialspoint.com/java/

https://www.javatpoint.com/java-programs

Page 5 of 5

w.e.f. AY 2018-19
List of Practical
S. S. AGRAWAL INSTITUTE OF ENGINEERING & TECHNOLOGY, NAVSARI

Computer Engineering Department


Subject with Code: Object Oriented Programming – I (3140705)

Sr.
Name of Experiment Remark
No.

1. Write programs for Operators and Expressions


1.1 Write a Program that displays Welcome to Java, Learning Java Now and
Programming is fun.
1.2 Write a program that solves the following equation and displays the value
x and y:

1) 3.4x+50.2y=44.5 2) 2.1x+.55y=5.9
(Assume Cramer’s rule to solve equation
ax+by=e x=ed-bf/ad-bc
cx+dy=f y=af-ec/ad-bc )
1.3 Write a program that reads a number in meters, converts it to feet, and
displays the result.

1.4 Body Mass Index (BMI) is a measure of health on weight. It can be


calculated by taking your weight in kilograms and dividing by the square
of your height in meters. Write a program that prompts the user to enter
a weight in pounds and height in inches and displays the BMI.

Note:- 1 pound=.45359237 Kg and 1 inch=.0254 meters.

1.5 (Sum the digits in an integer) Write a program that reads an integer
between 0 and 1000 and adds all the digits in the integer. For example, if
an integer is 932, the sum of all its digits is 14.
Hint: Use the % operator to extract digits, and use the / operator to
remove the extracted digit. For instance, 932 % 10 = 2 and 932 / 10 = 93.

1.6 Write a program that checks whether a number is divisible by 2 and 3,


by 2 or 3, and by 2 or 3 but not both.

2 Write programs for Selections , Mathematical functions and loops


2.1 Write a program that prompts the user to enter a weight in pounds and
height in inches and displays the BMI. Consider Following Conditions and
print the status of person:
bmi < 18.5 then "Underweight",

bmi < 25 "Normal"

bmi < 30 "Overweight"

bmi>30 "Obese"

2.2 Write a program that prompts the user to enter three integers and
display the integers in non-decreasing order.

2.3 (Random character) Write a program that displays a random uppercase


letter using the Math.random() method.

2.4 Write a program that prompts the user to enter a string and displays the
number of the uppercase letters in the string.

2.5 (Count vowels and consonants) Assume letters A, E, I, O, and U as the


vowels. Write a program that prompts the user to enter a string and displays
the number of vowels and consonants in the string.

3. Write Java program using Character, Array and String.

3.1 (Vowel or consonant?) Write a program that prompts the user to enter a
letter and check whether the letter is a vowel or consonant.

3.2 (Process a string) Write a program that prompts the user to enter a string
and displays its length and its first character.

3.3 (Generate vehicle plate numbers) Assume a vehicle plate number


consists of three uppercase letters followed by four digits. Write a program
to generate a plate number.

3.4 Write a program that reads an integer and displays all its smallest factors
in increasing order. For example if input number is 120, the output should
be as follows:2,2,2,3,5.

3.5 (Computing gcd) Write a method that returns the gcd of an unspecified
number of integers. The method header is specified as follows:

public static int gcd(int... numbers)

Write a test program that prompts the user to enter five numbers, invokes
the method to find the gcd of these numbers, and displays the gcd.

3.6 Write a test program that prompts the user to enter ten numbers, invoke
a method to reverse the numbers, display the numbers.
3.7 Write a program that generate 6*6 two-dimensional matrix, filled with
0’s and 1’s , display the matrix, check every raw and column have an odd
number’s of 1’s.

4 Write Java program using Methods, Classes, Objects and Constructor.

4.1 Write a method with following method header. public static int gcd(int
num1, int num2)

Write a program that prompts the user to enter two integers and compute
the gcd of two integers.

4.2 Write a program that creates three methods. The first finds the maximum
integer, the second finds the maximum double, and the third finds the
maximum among three double values. All three methods are named max.
(Method Overloading)

4.3 (Count the letters in a string) Write a method that counts the number of
letters in a string using the following header:

public static int countLetters(String s)

Write a test program that prompts the user to enter a string and displays the
number of letters in the string.

4.4 (The MyTriangle class) Create a class named MyTriangle that contains
the following two methods:

/** Return true if the sum of any two sides is greater than the third side.
*/

public static boolean isValid(double side1, double side2, double side3)

/** Return the area of the triangle. */

public static double area( double side1, double side2, double side3)

Write a test program that reads three sides for a triangle and computes the
area if the input is valid. Otherwise, it displays that the input is invalid.

4.5 (Use the Random class) Write a program that creates a Random object
with seed 1000 and displays the first 100 random integers between 1 and
49 using the NextInt (49) method.

5 Write a Java program using Encapsulation and Polymorphism


5.1 (The Loan class) Design a class named Loan. The class contains:
■ The data fields annualInterestRate, numberofYears, loanAmount amd
loanDate.

■ Three getter methods for the data fields annualInterestRate,


numberofYears, loanAmount amd loanDate respectively.

■ A method named Monthly_Payment() calculates the monthly payment of


Loan and another method named getTotalPayment() calculates Annula
Payment of Loan.

Draw the UML diagram for the class and then implement the class. Write a
test program that creates one Loan object.

5.2 Write a program for calculator to accept an expression as a string in


which the operands and operator are separated by zero or more spaces.

For ex: 3+4 and 3 + 4 are acceptable expressions.

5.3 Design a class named shape that extends three classes Rectangle, Circle
and Triangle. Call the overridden method named draw() through the
reference variable of a superclass. [ Runtime polymorphism or Dynamic
Method Dispatch)

5.4 Write a program that creates an Array List and adds a Loan object , a Date
object , a string, and a Circle object to the list, and use a loop to display all
elements in the list by invoking the object’s to String() method.

6. Write Java program using Exception Handling.


6.1 Write the bin2Dec (string binary String) method to convert a binary
string into a decimal number. Implement the bin2Dec method to throw a
NumberFormatException if the string is not a binary string.
6.2 Write a complete program to accept N integer numbers from the
command line. Raise and handle exceptions for following cases :
- when a number is –ve
- when a number is evenly divisible by 10
- when a number is greater than 1000 and less than 2000
- when a number is greater than 7000
Skip the number if an exception is raised for it, otherwise add it
to find total sum.
7. Write Java program using Inheritance, abstract class and Interface.

7.1 (The Triangle class) Design a class named Triangle that extends
GeometricObject. The class contains:
■ Three double data fields named side1, side2, and side3 with default values
1.0 to denote three sides of the triangle.
■ A no-arg constructor that creates a default triangle.
■ A constructor that creates a triangle with the specified side1, side2, and
side3.
■ The accessor methods for all three data fields.
■ A method named getArea() that returns the area of this triangle.
■ A method named getPerimeter() that returns the perimeter of this
triangle.
■ A method named toString() that returns a string description for the
triangle.

The toString() method is implemented as follows:


return "Triangle: side1 = " + side1 + " side2 = " + side2 +" side3 = " + side3;
Draw the UML diagrams for the classes Triangle and GeometricObject and
implement the classes. Write a test program that prompts the user to enter
three sides of the triangle, a color, and a Boolean value to indicate whether
the triangle is filled. The program should create a Triangle object with these
sides and set the color and filled properties using the input. The program
should display the area, perimeter, color, and true or false to indicate
whether it is filled or not.
7.2 Write a program to demonstrate combination of both types of inheritance
as shown in figure 1.
i.e.hybrid inheritance

7.3 Write an abstract class named Person and its two subclasses named
Student and Employee.
A person has a name, address, phone number, and email address. A student
has enrollment, course. An employee has an office, salary, and designation.
Define constructors and methods for input and display for both classes.
Write a main program to give demonstration of all.
7.4 (Convert decimals to fractions) Write a program that prompts the user to
enter a decimal number and displays the number in a fraction.
Hint: read the decimal number as a string, extract the integer part and
fractional part from the string, and use the BigInteger implementation of the
Rational class in Programming
8. Write a Java program using JAVAFX basics and Event-driven
programming and animations.
8.1 Write a program that displays a tic-tac-toe board. A cell may be X, O, or
empty. What to display at each cell is randomly decided. The X and O are
images in the files X.gif and O.gif.
8.2 Write a program that moves a circle up, down, left or right using arrow
keys.
8.3 Write a program that displays the color of a circle as red when the mouse
button is pressed and as blue when the mouse button is released.
9. Write a Java program using JAVAFX UI controls and multimedia.

9.1 Write a GUI program that use button to move the message to the left and
right and use the radio button to change the color for the message displayed.
10. Write a Java program using Binary I/O ,Recursion and Generic Class.

10.1 Write a program to create a file name 123.txt, if it does not exist. Append
a new data to it if it already exist. write 150 integers created randomly into
the file using Text I/O. Integers are separated by space.
10.2 Write a recursive method that returns the smallest integer in an array.
Write a test program that prompts the user to enter an integer and display
its product.
10.3 Write a generic method that returns the minimum elements in a two
dimensional array.
11. Write a Java Program using Collections

11.1 Write a program that reads words from a text file and displays all the
nonduplicate words in descending order.The text file is passed as a
command-line argument.
12. Write Java program using Threads

12.1 Define MYPriorityQueue class that extends Priority Queue to implement


the Cloneable interface and implement the clone() method to clone a priority
queue.
List of Assignment
S. S. A GRAWAL I NSTITUTE OF E NGINEERING & T ECHNOLOG Y , N AVS ARI
COMPUTER DEPARTMENT

Assignment No: 1
Unit 1 to 4
Date:
Sub
3140705 Title of Subject Object Oriented Programming - I
Code

Sr. No. Questions

1. What is the difference between OOP and procedural oriented language?

2. What are syntax errors (compile errors), runtime errors, and logic errors?

3. List OOP characteristics and describe inheritance with examples.

Write a single program which demonstrates the usage of following


keywords:
4.
i) import, ii) new, iii) this, iv) break, v) continue
Show how to compile and run the program in java
Write a program, which shows an example of function overloading. Also, differentiate
5
between function overloading and overriding
Explain the following:
6 i) Arguments and Parameters of a function
ii) Pass by Value and Pass by reference

7 Explain access modifiers with Example.

Explain the following constructors using appropriate example:


8
i) Default constructor and Parameterised constructor
ii) Shallow copy and Deep copy constructor

Consider class A as the parent of class B. Explain among the


following which statement will show the compilation error.

9 i) A a = new A();
ii) A a = new B();
iii) B b = new A();
iv) B b = new B();
Page1
S. S. A GRAWAL I NSTITUTE OF E NGINEERING & T ECHNOLOG Y , N AVS ARI
COMPUTER DEPARTMENT

10
Explain static keyword with example.

Ms. Farheen Mansur


Name of Faculty
Ms. Bhagyasri Patel

Date of Submission

Sign of Faculty

Page2
S. S. A GRAWAL I NSTITUTE OF E NGINEERING & T ECHNOLOG Y , N AVS ARI
COMPUTER DEPARTMENT

Assignment No: 2
Unit 5 and 6
Date:
Sub
3140705 Title of Subject Object Oriented Programming - I
Code

Sr. No. Questions

1. Explain inheritance with its types and give suitable example

Describe abstract class called Shape, which has three subclasses say Triangle, Rectangle,
and Circle. Define one method area() in the abstract class and override this area() in
2.
these three subclasses to calculate for specific object i.e. area() of Triangle subclass
should calculate area of triangle likewise for Rectangle and Circle

3. Explain the words super, static, final and this with the help of an example.

4. Explain Primitive data type and wrapper class data types

List out different methods available for String class in java and explain any two with
5
proper example

6 What is Dynamic binding? Show with an example how dynamic binding works.

Write a method for computing xy doing repetitive multiplication. X and y are of type
7 integer and are to be given as command line arguments. Raise and handle exception(s)
for invalid values of x and y.

8 Demonstrate use of try-catch construct in case of hierarchical Exception Handling. (i.e


handling various exception belongs to the exception hierarchy)
Write a program that counts the number of words in a text file. The file name is passed
9
as a command line argument. The words in the file are separated by white space
characters.
10 Create a class called Student. Write a student manager program to manipulate the
student information from files by using FileInputStream and FileOutputStream.

Ms. Farheen Mansur


Name of Faculty
Ms. Bhagyasri Patel

Date of Submission

Sign of Faculty
Page1
S. S. A GRAWAL I NSTITUTE OF E NGINEERING & T ECHNOLOG Y , N AVS ARI
COMPUTER DEPARTMENT

Assignment No: 3
Unit 7 to 12
Date:
Sub
3140705 Title of Subject Object Oriented Programming - I
Code

Sr. No. Questions


How do you create a Scene object? How do you set a scene in a stage?
1. Is it possible to create multiple scenes? Write a program to place a circle in the scene and
fill circle with red color.

2. Write a short note on JAVAFX controls

3. Enlist various layout panes and explain any two in detail.

4. Explain following controls


1) text-Area 2) scrollbar 3) checkbox 4) combo-box
5 Demonstrate animation effect in JavaFX.

6 Write short notes about I/O stream classes

Write a program to add input elements in ArrayList collection class, then sort the
7 inserted elements in descending order and display the sorted output.
hint: use Collections.reverseOrder()

8 Explain Set and Map in Java with example

9
Explain the thread state, thread properties and thread synchronization
10 Explain Thread life cycle in detail. Write a program to create a child thread to print
integer numbers 1 to 10.

Ms. Farheen Mansur


Name of Faculty
Ms. Bhagyasri Patel

Date of Submission

Sign of Faculty
Page1
Last Five-Year Question
Papers
Seat No.: ________ Enrolment No.___________
GUJARAT TECHNOLOGICAL UNIVERSITY
BE - SEMESTER– IV EXAMINATION – SUMMER 2020
Subject Code: 3140705 Date:26/10/2020
Subject Name: Object Oriented Programming -I
Time: 10:30 AM TO 01:00 PM Total Marks: 70
Instructions:
1. Attempt all questions.
2. Make suitable assumptions wherever necessary.
3. Figures to the right indicate full marks.
MARKS
Q.1 (a) Explain JRE, JDK and JIT. 03
(b) Explain static keyword with example. 04
(c) Explain inheritance with its types and give suitable example. 07

Q.2 (a) Compare object-oriented programming with sequential programming. 03


(b) Method main is a public static method. Justify 04
(c) Write a program, which shows an example of function overloading. 07
Also, differentiate between function overloading and overriding.
OR
(c) Write a program to take string input as command line argument. In 07
addition, count occurrence of each character in a given string.
Q.3 (a) Write difference between String class and StringBuffer class. 03
(b) Explain super keyword with example. 04
(c) Describe abstract class called Shape, which has three subclasses say 07
Triangle, Rectangle, and Circle. Define one method area() in the abstract
class and override this area() in these three subclasses to calculate for
specific object i.e. area() of Triangle subclass should calculate area of
triangle likewise for Rectangle and Circle.
OR
Q.3 (a) How can we protect sub class to override the method of super class? 03
Explain with example.
(b) Define Interface and explain how it differs from the class. 04
(c) What do you mean by run time polymorphism? Write a program to 07
demonstrate run time polymorphism.
Q.4 (a) Differentiate between Text I/O and Binary I/O. 03
(b) Explain ArrayList class. 04
(c) What is an Exception? List out various built-in exceptions in JAVA and 07
explain any one Exception class with suitable example.
OR
Q.4 (a) How do you declare a generic type in a class? Explain. 03
(b) Write a JAVA program to read student.txt file and display the content. 04
(c) Explain Thread life cycle in detail. Write a program to create a child 07
thread to print integer numbers 1 to 10.
Q.5 (a) Explain in brief: Color class and its methods. 03
(b) What method do you use to obtain an element in the collection from an 04
iterator? Explain with example.
(c) Enlist various layout panes and explain any two in detail. 07
OR
Q.5 (a) Compare Set and List interfaces. 03
(b) Write importance of JAVAFX compare to AWT and Swing. 04
(c) How do you create a Scene object? How do you set a scene in a stage? 07
Is it possible to create multiple scenes? Write a program to place a circle
in the scene and fill circle with red color.

1
Seat No.: ________ Enrolment No.___________

GUJARAT TECHNOLOGICAL UNIVERSITY


BE - SEMESTER–IV (NEW) EXAMINATION – SUMMER 2021
Subject Code:3140705 Date:11/09/2021
Subject Name:Object Oriented Programming -I
Time:02:30 PM TO 05:00 PM Total Marks:70
Instructions:
1. Attempt all questions.
2. Make suitable assumptions wherever necessary.
3. Figures to the right indicate full marks.
4. Simple and non-programmable scientific calculators are allowed.

MARKS
Q.1 (a) Define object oriented concepts. 03
(b) What is the difference between the StringBuffer and StringBuilder 04
classes?
(c) Define constructor. How objects are constructed? Explain constructor 07
overloading with an example.

Q.2 (a) Explain about arrays, Type of arrays and arrays methods. 03
(b) Explain about Encapsulation, Abstraction. 04
(c) State the design hints for class and inheritance. 07
Also discuss the working and meaning of the “static” modifier with
suitable examples.
OR
(c) Explain in detail how inheritance and polymorphism are supported in 07
java with necessary examples.
Q.3 (a) Explain about different types of string methods. 03
(b) Write short notes on access specifiers and modifiers in java. 04
(c) What is an Exception? Explain the exception hierarchy. Explain how 07
to throw, catch and handle Exceptions.
OR
Q.3 (a) Explain about Final class, Fields, Methods. 03
(b) What is a Package? What are the benefits of using packages? Write 04
down the steps in creating a package and using it in a java program
with an example.
(c) Explain the concept of inner classes and explain the types of inner 07
classes with an example program.
Q.4 (a) What is Dynamic binding? Show with an example how dynamic 03
binding works.
(b) Write short notes about I/O stream classes. 04
(c) Explain the thread state, thread properties and thread synchronization. 07
OR
Q.4 (a) Explain the concept of finalization. 03
(b) What is reflection and how does it help to manipulate java code. 04
(c) Write a java program to implement the multiple inheritance concepts 07
for calculating area of circle and square.
Q.5 (a) Explain about callback 03
(b) Explain the interface with an example program. 04
(c) What is Generic programming and why is it needed? Explain with 07
example. List the limitations and restrictions of generic programming
1
OR
Q.5 (a) Explain about Proxy class, Interface and Methods. 03
(b) Explain about adapter classes and mouse events with an example. 04
(c) With a neat diagram explain the Model view controller design pattern 07
and list out the advantages and disadvantages of using it in designing
an application.

*************

2
Seat No.: ________ Enrolment No.___________

GUJARAT TECHNOLOGICAL UNIVERSITY


BE - SEMESTER–IV (NEW) EXAMINATION – SUMMER 2022
Subject Code:3140705 Date:08-07-2022
Subject Name:Object Oriented Programming -I
Time:10:30 AM TO 01:00 PM Total Marks: 70
Instructions:
1. Attempt all questions.
2. Make suitable assumptions wherever necessary.
3. Figures to the right indicate full marks.
4. Simple and non-programmable scientific calculators are allowed.
Marks
Q.1 (a) List out features of Java. Explain any two features. 3
(b) Write a single program which demonstrates the usage of following 4
keywords:
i) import, ii) new, iii) this, iv) break, v) continue
Show how to compile and run the program in java.
(c) Demonstrate use of try-catch construct in case of hierarchical 7
Exception Handling. (i.e handling various exception belongs to the
exception hierarchy)

Q.2 (a) Explain following Java keywords using appropriate examples: 3


i) static, ii) final, iii) super
(b) Consider class A as the parent of class B. Explain among the 4
following which statement will show the compilation error.
i) A a = new A();
ii) A a = new B();
iii) B b = new A();
iv) B b = new B();
(c) Write a java program to take infix expressions and convert it into 7
prefix expressions.
OR
(c) Write a java program that evaluates a math expression given in string 7
form from command line arguments.

Q.3 (a) Defines types of inheritance. 3


(b) Explain the following constructors using appropriate example: 4
i) Default constructor and Parameterised constructor
ii) Shallow copy and Deep copy constructor
(c) Explain file io using byte stream with appropriate example. 7
hint: use FileInputStream, FileOutputStream
OR
Q.3 (a) Define types of polymorphism 3
(b) Explain the following: 4
i) Arguments and Parameters of a function
ii) Pass by Value and Pass by reference
1
(c) Explain file io using character stream with appropriate example. 7
hint: use FileReader, FileWriter

Q.4 (a) Define Encapsulation and access specifier 3


(b) Explain multithreading using Thread class 4
(c) Write a short note on Java Collections. 7
OR
Q.4 (a) Differentiate between Abstract class and Interfaces 3
(b) Explain multithreading using Runnable interface 4
(c) Write a program to add input elements in ArrayList collection class, 7
then sort the inserted elements in descending order and display the
sorted output.
hint: use Collections.reverseOrder()

Q.5 (a) Explain following Java keywords using appropriate examples: 3


i) throw, ii) throws, iii) finally
(b) In multi-threading using Thread class, explain with an example how 4
a start() method call invokes the run method of the class extending
Thread class.
(c) Write a short note on JAVAFX controls. 7
OR
Q.5 (a) Explain thread life cycle 3
(b) In multi-threads using the Runnable interface, explain with an 4
example how a start() method calls the run() method of a class
implementing a runnable interface.
(c) Develop a GUI based application using JAVAFX controls. 7

*************

2
Seat No.: ________ Enrolment No.___________

GUJARAT TECHNOLOGICAL UNIVERSITY


BE- SEMESTER–IV (NEW) EXAMINATION – WINTER 2020
Subject Code:3140705 Date:24/02/2021
Subject Name:Object Oriented Programming -I
Time:02:30 PM TO 04:30 PM Total Marks:56
Instructions:
1. Attempt any FOUR questions out of EIGHT questions.
2. Make suitable assumptions wherever necessary.
3. Figures to the right indicate full marks.

Q.1 (a) What is the difference between oop and procedural oriented language? 03
(b) Explain type-conversion in java. 04
(c) Explain class and object with respect to java. 07

Q.2 (a) Difference between Nested if and multi-way if statements. 03


(b) What is constructor overloading? 04
(c) What are the data-types and operators available in Java? 07

Q.3 (a) Explain access modifiers with Example. 03


(b) Explain following keywords 04
1) Static 2) Super
(c) Differentiate between final, finally and finalize. What will happen if we make 07
class and method as final?

Q.4 (a) Explain abstract class with example. 03


(b) Explain Primitive data type and wrapper class data types. 04
(c) Write a program to rise and handle divide by zero exception. 07

Q.5 (a) Explain mouse and key event handler in JavaFX. 03


(b) Explain following controls 04
1) text-Area 2) scrollbar 3) checkbox 4) combo-box
(c) Write a method for computing xy doing repetitive multiplication. X and y are 07
of type integer and are to be given as command line arguments. Raise and
handle exception(s) for invalid values of x and y.

Q.6 (a) Explain following classes in JavaFX. 03


1) Color class 2) font class 3) Image and image view class
(b) What is Exception? Demonstrate how you can handle different types of 04
exception separately.
(c) List and explain available type of constructors in java with example. 07

Q.7 (a) Differentiate Text I/O and Binary I/O. 03


(b) Explain usage of class FileInputStream and FileOutputStream by giving an 04
example.
(c) Explain Comparable and Cloneable interface. 07

Q.8 (a) What is Collection? Explain list, stack, queue classes. 03


(b) How to access object via reference variable? Explain with example. 04
(c) What do you understand by thread? Describe the complete life cycle of 07
thread.

*************

1
Seat No.: ________ Enrolment No.___________

GUJARAT TECHNOLOGICAL UNIVERSITY


BE - SEMESTER–IV (NEW) EXAMINATION – WINTER 2021
Subject Code:3140705 Date:05/01/2022
Subject Name:Object Oriented Programming -I
Time:10:30 AM TO 01:00 PM Total Marks: 70
Instructions:
1. Attempt all questions.
2. Make suitable assumptions wherever necessary.
3. Figures to the right indicate full marks.
4. Simple and non-programmable scientific calculators are allowed.

MARKS
Q.1 (a) What are syntax errors (compile errors), runtime errors, and logic 03
errors?
(b) What is Type Casting? Explain widening and narrowing type 04
casting.
(c) Explain Data Types in detail with example. 07

Q.2 (a) What is an interface? Explain with example. 03


(b) Explain “Passing argument by values” with example. 04
(c) Explain File class with its methods. 07
OR
(c) What is polymorphism? Explain dynamic binding with example. 07
Q.3 (a) Explain abstract class with example. 03
(b) Explain Method Overloading and Overriding. 04
(c) Write a program that counts the number of words in a text file. 07
The file name is passed as a command line argument. The words
in the file are separated by white space characters.
OR
Q.3 (a) Differentiate String class and StringBuffer class. 03
(b) Explain following keywords 04
(1) super (2) this
(c) Write a program that illustrates interface inheritance. Interface P 07
is extended by P1 and P2. Interface P12 inherits from both P1 and
P2. Each interface declares one constant and one method. class Q
implements P12. Instantiate Q and invoke each of its methods.
Each method displays one of the constants.
Q.4 (a) Explain visibility modifiers. 03
(b) Explain following controls 04
(1) Checkbox (2) Radio Button (3) Textfield (4) Label
(c) What is an Exception? Explain try, catch and finally with 07
example.
OR
Q.4 (a) What is the keyword “throw” used for? What is the keyword 03
“throws” used for?
(b) What is constructor? Explain constructor overloading. 04
(c) Explain different layout panes used in JavaFX. 07
Q.5 (a) Differentiate Text I/O and Binary I/O 03
(b) Explain Inner class with example. 04
(c) What is thread? Describe the complete life cycle of thread. 07
OR

1
Q.5 (a) Compare List and Set. 03
(b) Explain static variable and static method with example. 04
(c) Explain Thread Synchronization with example. 07

*************

2
Seat No.: ________ Enrolment No.___________

GUJARAT TECHNOLOGICAL UNIVERSITY


BE - SEMESTER–IV(NEW) EXAMINATION – WINTER 2022
Subject Code:3140705 Date:20-12-2022
Subject Name:Object Oriented Programming -I
Time:10:30 AM TO 01:00 PM Total Marks:70
Instructions:
1. Attempt all questions.
2. Make suitable assumptions wherever necessary.
3. Figures to the right indicate full marks.
4. Simple and non-programmable scientific calculators are allowed.
MARKS
Q.1 (a) Discuss significance of byte code. 03
(b) Explain Java garbage collection mechanism. 04
(c) List OOP characteristics and describe inheritance with examples. 07

Q.2 (a) Explain constructor with the help of an example. 03


(b) List out different methods available for String class in java and explain 04
any two with proper example.
(c) Explain all access modifiers and their visibility as class members. 07
OR
(c) Compare String with StringBuffer. Also, write a program to count the 07
occurrence of a character in a string.

Q.3 (a) What is the final class? Why they are used? 03
(b) Write exception handling mechanisms in JAVA. 04
(c) Explain Overloading and Overriding with example. 07
OR
Q.3 (a) How can you create packages in Java? 03
(b) What is Inheritance? List out the different forms of Inheritance and 04
explain any one with example.
(c) Explain the words super, static, final and this with the help of an 07
example.

Q.4 (a) List out various layout panes in JavaFX. 03


(b) Explain the architecture of JavaFX. 04
(c) Discuss BufferedInputStream and BufferedOutputStream classes with 07
an example.
OR
Q.4 (a) List out JavaFX UI controls and explain any one in detail. 03
(b) Demonstrate animation effect in JavaFX. 04
(c) Create a class called Student. Write a student manager program to 07
manipulate the student information from files by using FileInputStream
and FileOutputStream.

Q.5 (a) What is Java Collection? 03


(b) List out methods of Iterator and explain it. 04
(c) Explain Set and Map in Java with example. 07
OR
Q.5 (a) What is Vector class? 03
(b) Describe with diagram the life cycle of Thread. 04
(c) Explain synchronization in Thread with suitable example. 07
***********
1
Question bank
S. S. AGRAWAL INSTITUTE OF ENGINEERING & TECHNOLOGY, NAVSARI
DEPARTMENT OF COMPUTER ENGINEERING

Subject: 3140705 (Object Oriented Programming -I)


Question Bank

Sr. No. Questions Exam Marks

Unit 1: Introduction to java and elementary programming

1 Explain JRE, JDK and JIT. SUMMER2020 03

Compare object-oriented programming with sequential SUMMER2020 03


programming WINTER 2020
2 OR
What is the difference between oop and procedural oriented
language?

3 Method main is a public static method. Justify SUMMER2020 04

4 Explain type-conversion in java. WINTER 2020 04

5 What are the data-types and operators available in Java? WINTER 2020 07

6 Define object oriented concepts. SUMMER 2021 03

What are syntax errors (compile errors), runtime errors, and WINTER 2021 03
7
logic errors?
What is Type Casting? Explain widening and narrowing type WINTER 2021 04
8
casting.

9 Explain Data Types in detail with example WINTER 2021 07

10 List out features of Java. Explain any two features. SUMMER 2022 03

11 Discuss significance of byte code. WINTER 2022 03

12 Explain Java garbage collection mechanism. WINTER 2022 04

List OOP characteristics and describe inheritance with examples. WINTER 2022 07

Object Oriented Programming -I


S. S. AGRAWAL INSTITUTE OF ENGINEERING & TECHNOLOGY, NAVSARI
DEPARTMENT OF COMPUTER ENGINEERING

Unit 2: Selections , Mathematical functions and loops

Write a program to take string input as command line SUMMER2020 07


1 argument. In addition, count occurrence of each character in a
given string.

2 Difference between Nested if and multi-way if statements. WINTER 2020 03

Write a single program which demonstrates the usage of SUMMER 2022 04


following
3 keywords:
i) import, ii) new, iii) this, iv) break, v) continue
Show how to compile and run the program in java

Unit 3: Methods and Arrays:

Write a program, which shows an example of function SUMMER2020 07


1 overloading. Also, differentiate between function overloading
and overriding

2 Explain about arrays, Type of arrays and arrays methods SUMMER 2021 03

3 Explain “Passing argument by values” with example WINTER 2021 04

4 Explain Method Overloading and Overriding WINTER 2021 04

Explain the following: SUMMER 2022 04


5 i) Arguments and Parameters of a function
ii) Pass by Value and Pass by reference

6 Explain Overloading and Overriding with example. WINTER 2022 07

Unit 4: Objects and Classes

1 Explain static keyword with example. SUMMER2020 04

2 WINTER 2020 07
Explain class and object with respect to java.
3 WINTER 2020 04
What is constructor overloading?

4 List and explain available type of constructors in java with WINTER 2020 04
example

Object Oriented Programming -I


S. S. AGRAWAL INSTITUTE OF ENGINEERING & TECHNOLOGY, NAVSARI
DEPARTMENT OF COMPUTER ENGINEERING

5 How to access object via reference variable? Explain with WINTER 2020 04
example.
6 Define constructor. How objects are constructed? Explain SUMMER 2021 07
constructor overloading with an example.
What is a Package? What are the benefits of using packages? SUMMER 2021
7
Write down the steps in creating a package and using it in a
java program with an example.
Explain access modifiers with Example. WINTER 2020 03
8 OR
Write short notes on access specifiers and modifiers in java. SUMMER 2021 04

9 WINTER 2021
Explain visibility modifiers. 03

10 WINTER 2021 04
What is constructor? Explain constructor overloading
SUMMER 2022
Consider class A as the parent of class B. Explain among the 04
following which statement will show the compilation error.

11 i) A a = new A();
ii) A a = new B();
iii) B b = new A();
iv) B b = new B();
SUMMER 2022
Explain the following constructors using appropriate 04
example:
12
i) Default constructor and Parameterised constructor
ii) Shallow copy and Deep copy constructor
13 WINTER 2022
Explain constructor with the help of an example 03
WINTER 2022
14 Explain all access modifiers and their visibility as class 07
members.

Object Oriented Programming -I


S. S. AGRAWAL INSTITUTE OF ENGINEERING & TECHNOLOGY, NAVSARI
DEPARTMENT OF COMPUTER ENGINEERING

Unit 5: Object oriented thinking


Explain inheritance with its types and give suitable example SUMMER2020 07
1 OR
Defines types of inheritance. SUMMER 2022
04
SUMMER2020 03
2 Write difference between String class and StringBuffer class. SUMMER 2021 04
WINTER 2021 03
SUMMER2020 04
3 Explain super keyword with example

Describe abstract class called Shape, which has three SUMMER2020 07


subclasses say Triangle, Rectangle, and Circle. Define one
method area() in the abstract class and override this area() in
4
these three subclasses to calculate for specific object i.e. area()
of Triangle subclass should calculate area of triangle likewise
for Rectangle and Circle
How can we protect sub class to override the method of super SUMMER2020 03
5
class? Explain with example.
What do you mean by run time polymorphism? Write a SUMMER2020 07
6
program to demonstrate run time polymorphism
Explain following keywords WINTER 2020 04
1) Static 2) Super WINTER2022 07
7 OR
Explain the words super, static, final and this with the help of
an example.
Differentiate between final, finally and finalize. What will WINTER 2020 07
8
happen if we make class and method as final?
Explain abstract class with example. WINTER 2020 03
9 OR
Explain abstract class with example. WINTER 2021 03
10 Explain Primitive data type and wrapper class data types. WINTER 2020 04

11 Explain about Encapsulation, Abstraction SUMMER 2021 04

Object Oriented Programming -I


S. S. AGRAWAL INSTITUTE OF ENGINEERING & TECHNOLOGY, NAVSARI
DEPARTMENT OF COMPUTER ENGINEERING

State the design hints for class and inheritance. SUMMER 2021 07
12 Also discuss the working and meaning of the “static” modifier
with suitable examples.
Explain about different types of string methods. SUMMER 2021 07
OR
13 WINTER 2022 04
List out different methods available for String class in java
and explain any two with proper example
Explain about Final class, Fields, Methods SUMMER 2021 03
14 OR
WINTER 2022 03
What is the final class? Why they are used?
What is Dynamic binding? Show with an example how SUMMER 2021 03
15
dynamic binding works.
16 Explain the concept of finalization. SUMMER 2021 03

Write a java program to implement the multiple inheritance SUMMER 2021 07


17
concepts for calculating area of circle and square.
What is polymorphism? Explain dynamic binding with WINTER 2021 07
18
example.
Explain following keywords WINTER 2021 04
19
(1) super (2) this
20 Define types of polymorphism SUMMER 2022 03

21 Differentiate between Abstract class and Interfaces SUMMER 2022 03

Compare String with StringBuffer. Also, write a program to WINTER 2022 07


22
count the occurrence of a character in a string.
Discuss BufferedInputStream and BufferedOutputStream WINTER 2022 07
23
classes with an example.

Object Oriented Programming -I


S. S. AGRAWAL INSTITUTE OF ENGINEERING & TECHNOLOGY, NAVSARI
DEPARTMENT OF COMPUTER ENGINEERING

Unit 6: Exception Handling, I/O, abstract classes and interfaces

1 Define Interface and explain how it differs from the class SUMMER2020 04

What is an Exception? List out various built-in exceptions in SUMMER2020 07


2 JAVA and explain any one Exception class with suitable
example.
WINTER 2020 07
3 Write a program to rise and handle divide by zero exception.

Write a method for computing xy doing repetitive WINTER 2020 07


multiplication. X and y are of type integer and are to be given
4
as command line arguments. Raise and handle exception(s) for
invalid values of x and y.
What is Exception? Demonstrate how you can handle WINTER 2020 04
6
different types of exception separately.
WINTER 2020 07
7 Explain Comparable and Cloneable interface.

Explain in detail how inheritance and polymorphism are SUMMER 2021 07


8
supported in java with necessary examples
What is an Exception? Explain the exception hierarchy. SUMMER 2021 07
Explain how to throw, catch and handle Exceptions
OR
9 Demonstrate use of try-catch construct in case of hierarchical SUMMER 2022 07
Exception Handling. (i.e handling various exception belongs
to the
exception hierarchy)
SUMMER 2021 07
10 Explain the interface with an example program. WINTER 2021

07
11 Explain File class with its methods.

Write a JAVA program to read student.txt file and display the SUMMER2020 04
12
content

Explain usage of class FileInputStream and FileOutput-Stream WINTER 2020 04


13
by giving an example.

Object Oriented Programming -I


S. S. AGRAWAL INSTITUTE OF ENGINEERING & TECHNOLOGY, NAVSARI
DEPARTMENT OF COMPUTER ENGINEERING

Write a program that counts the number of words in a text WINTER 2021 07
14 file. The file name is passed as a command line argument. The
words in the file are separated by white space characters.
Write a program that illustrates interface inheritance. WINTER 2021 07
Interface P is extended by P1 and P2. Interface P12 inherits
15 from both P1 and P2. Each interface declares one constant
and one method. class Q implements P12. Instantiate Q and
invoke each of its methods. Each method displays one of the
constants.
What is an Exception? Explain try, catch and finally with WINTER 2021 07
16
example.

What is the keyword “throw” used for? What is the keyword WINTER 2021 03
17
“throws” used for?
Write a java program that evaluates a math expression given SUMMER 2022 07
18 in string
form from command line arguments.
Explain file io using byte stream with appropriate example. SUMMER 2022 07
19
hint: use FileInputStream, FileOutputStream
Explain file io using character stream with appropriate SUMMER 2022 04
20 example.
hint: use FileReader, FileWriter
Explain following Java keywords using appropriate examples: SUMMER 2022 03
21
i) throw, ii) throws, iii) finally
WINTER 2022 04
22 Write exception handling mechanisms in JAVA.

Create a class called Student. Write a student manager program to WINTER 2022 07
23 manipulate the student information from files by using
FileInputStream and FileOutputStream.

Object Oriented Programming -I


S. S. AGRAWAL INSTITUTE OF ENGINEERING & TECHNOLOGY, NAVSARI
DEPARTMENT OF COMPUTER ENGINEERING

Unit 7: JAVAFX basics and Event-driven programming and animations

1 Explain in brief: Color class and its methods. SUMMER2020 03

Enlist various layout panes and explain any two in detail. SUMMER2020 07
2 OR WINTER 2021 07
List out various layout panes in JavaFX. WINTER 2022 03
3 Write importance of JAVAFX compare to AWT and Swing. SUMMER2020 04

How do you create a Scene object? How do you set a scene SUMMER2020 07
in a stage?
4
Is it possible to create multiple scenes? Write a program to
place a circle in the scene and fill circle with red color.

5 WINTER 2020 03
Explain mouse and key event handler in JavaFX

6 Explain following classes in JavaFX. WINTER 2020 03


1) Color class 2) font class 3) Image and image view class
7 Explain the concept of inner classes and explain the types of SUMMER2021 07
inner classes with an example program.
8 Explain about adapter classes and mouse events with an SUMMER2021 04
example.
9 WINTER 2021 04
Explain Inner class with example
10 SUMMER2022 07
Write a short note on JAVAFX controls
11 WINTER 2022 07
Explain the architecture of JavaFX

Unit 8:JAVAFX UI controls and multimedia


Explain following controls WINTER 2020 04
1
1) text-Area 2) scrollbar 3) checkbox 4) combo-box
Explain following controls WINTER 2021 04
2
(1) Checkbox (2) Radio Button (3) Textfield (4) Label
SUMMER2022 07
3 Develop a GUI based application using JAVAFX controls.
WINTER 2022 03
4 List out JavaFX UI controls and explain any one in detail.
WINTER 2022 04
5 Demonstrate animation effect in JavaFX.

Object Oriented Programming -I


S. S. AGRAWAL INSTITUTE OF ENGINEERING & TECHNOLOGY, NAVSARI
DEPARTMENT OF COMPUTER ENGINEERING

Unit 9: Binary I/O ,Recursion and Generics


Differentiate between Text I/O and Binary I/O. SUMMER2020 04
1
WINTER 2021
SUMMER2020 03
2 How do you declare a generic type in a class? Explain.
SUMMER2021 04
3 Write short notes about I/O stream classes
What is Generic programming and why is it needed? Explain SUMMER2021 03
4 with example. List the limitations and restrictions of generic
programming
SUMMER2021 04
5 Explain about Proxy class, Interface and Methods.

Unit 10: List, Stacks, Queues and Priority Queues


SUMMER2020 04
1 Explain ArrayList class
What method do you use to obtain an element in the SUMMER2020 04
2
collection from an iterator? Explain with example.
What is Collection? Explain list, stack, queue classes. WINTER 2020 03
3 OR
What is Java Collection? WINTER 2022 03
SUMMER 2022 07
4 Write a short note on Java Collections
Write a program to add input elements in ArrayList SUMMER 2022 07
collection class, then sort the inserted elements in
5
descending order and display the sorted output.
hint: use Collections.reverseOrder()
WINTER 2022 04
6 List out methods of Iterator and explain it.
WINTER 2022 03
7 What is Vector class?

Unit 11: Sets and Maps


SUMMER2020 03
1 Compare Set and List interfaces.
07
2 Explain Set and Map in Java with example

Unit 12: Concurrency


Explain Thread life cycle in detail. Write a program to SUMMER2020 07
1
create a child thread to print integer numbers 1 to 10.
What do you understand by thread? Describe the complete WINTER 2020 07
2
life cycle of thread.
Explain the thread state, thread properties and thread SUMMER2021 04
3
synchronization

Object Oriented Programming -I


S. S. AGRAWAL INSTITUTE OF ENGINEERING & TECHNOLOGY, NAVSARI
DEPARTMENT OF COMPUTER ENGINEERING

WINTER 2021 07
4 What is thread? Describe the complete life cycle of thread.
WINTER 2021 07
5 Explain Thread Synchronization with example.
WINTER 2022
SUMMER 2022 04
6 Explain multithreading using Thread class
SUMMER 2022
7 Explain multithreading using Runnable interface
In multi-threading using Thread class, explain with an SUMMER 2022 04
8 example how a start() method call invokes the run method
of the class extending Thread class.
In multi-threads using the Runnable interface, explain with SUMMER 2022 04
9 an example how a start() method calls the run() method of
a class implementing a runnable interface.

Name of Faculty Ms. B. G. Patel

Date of Submission

Sign of Faculty

Object Oriented Programming -I


Frequently asked questions
with answer
S. S. AGRAWAL INSTITUTE OF ENGINEERING & TECHNOLOGY, NAVSARI
DEPARTMENT OF COMPUTER ENGINEERING

Subject: 3140705 (Object Oriented Programming -I)


Most Repeated Questions

Sr. No. Questions Exam Marks

Compare object-oriented programming with sequential SUMMER2020 03


programming WINTER 2020
1 OR
What is the difference between oop and procedural oriented
language?
Write a program, which shows an example of function SUMMER2020 07
2 overloading. Also, differentiate between function overloading
and overriding
Explain access modifiers with Example. WINTER 2020 03
3 OR
Write short notes on access specifiers and modifiers in java. SUMMER 2021 04
Explain inheritance with its types and give suitable example SUMMER2020 07
4 OR
Defines types of inheritance. SUMMER 2022 04

SUMMER2020 03
5 Write difference between String class and StringBuffer class. SUMMER 2021 04
WINTER 2021 03
Explain following keywords WINTER 2020 04
1) Static 2) Super WINTER2022 07

6 OR
Explain the words super, static, final and this with the help of
an example.
Explain abstract class with example. WINTER 2020 03
7 OR
Explain abstract class with example. WINTER 2021 03
Explain about different types of string methods. SUMMER 2021 07
8
OR
WINTER 2022 04

Object Oriented Programming -I


S. S. AGRAWAL INSTITUTE OF ENGINEERING & TECHNOLOGY, NAVSARI
DEPARTMENT OF COMPUTER ENGINEERING

List out different methods available for String class in java


and explain any two with proper example
What is an Exception? Explain the exception hierarchy. SUMMER 2021 07
Explain how to throw, catch and handle Exceptions
OR
9 Demonstrate use of try-catch construct in case of hierarchical SUMMER 2022 07
Exception Handling. (i.e handling various exception belongs
to the
exception hierarchy)
10 SUMMER 2021 07
Explain the interface with an example program.
WINTER 2021
Enlist various layout panes and explain any two in detail. SUMMER2020 07
11 OR WINTER 2021 07
List out various layout panes in JavaFX. WINTER 2022 03
12 WINTER 2021 07
Explain Thread Synchronization with example.
WINTER 2022

Object Oriented Programming -I


S. S. AGRAWAL INSTITUTE OF ENGINEERING & TECHNOLOGY, NAVSARI
DEPARTMENT OF COMPUTER ENGINEERING

Answers:

1) What is the difference between OOP and procedural oriented language?

Procedural Oriented Programming Object-Oriented Programming

In object-oriented programming, the


In procedural programming, the program is program is divided into small parts
divided into small parts called functions. called objects.

Procedural programming follows a top-down Object-oriented programming follows


approach. a bottom-up approach.

Object-oriented programming has access


There is no access specifier in procedural specifiers like private, public, protected,
programming. etc.

Adding new data and functions is not easy. Adding new data and function is easy.

Procedural programming does not have any Object-oriented programming provides


proper way of hiding data so it is less secure. data hiding so it is more secure.

In procedural programming, overloading is not Overloading is possible in object-oriented


possible. programming.

In object-oriented programming, the


In procedural programming, there is no concept concept of data hiding and inheritance is
of data hiding and inheritance. used.

In procedural programming, the function is In object-oriented programming, data is


more important than the data. more important than function.

Procedural programming is based on the unreal Object-oriented programming is based on


world. the real world.

Procedural programming is used for designing Object-oriented programming is used for


medium-sized programs. designing large and complex programs.

Object Oriented Programming -I


S. S. AGRAWAL INSTITUTE OF ENGINEERING & TECHNOLOGY, NAVSARI
DEPARTMENT OF COMPUTER ENGINEERING

Procedural Oriented Programming Object-Oriented Programming

Procedural programming uses the concept of Object-oriented programming uses the


procedure abstraction. concept of data abstraction.

Code reusability absent in procedural Code reusability present in object-


programming, oriented programming.

Examples: C, FORTRAN, Pascal, Basic, etc. Examples: C++, Java, Python, C#, etc.

2) Write a program, which shows an example of function overloading. Also, differentiate between
function overloading and overriding

Method Overloading Method Overriding

Method overloading is a compile-time Method overriding is a run-time


polymorphism. polymorphism.

It is used to grant the specific implementation


It helps to increase the readability of the of the method which is already provided by its
program. parent class or superclass.

It is performed in two classes with inheritance


It occurs within the class. relationships.

Method overloading may or may not require


inheritance. Method overriding always needs inheritance.

In method overloading, methods must have In method overriding, methods must have the
the same name and different signatures. same name and same signature.

In method overloading, the return type can


or can not be the same, but we just have to In method overriding, the return type must be
change the parameter. the same or co-variant.

Static binding is being used for overloaded Dynamic binding is being used for overriding
methods. methods.

Object Oriented Programming -I


S. S. AGRAWAL INSTITUTE OF ENGINEERING & TECHNOLOGY, NAVSARI
DEPARTMENT OF COMPUTER ENGINEERING

Method Overloading Method Overriding

It gives better performance. The reason behind


Poor Performance due to compile time this is that the binding of overridden methods
polymorphism. is being done at runtime.

Private and final methods can be


overloaded. Private and final methods can’t be overridden.

Argument list should be different while Argument list should be same in method
doing method overloading. overriding.

Example of Function Overloading:

public class OverloadedMethod


{
public int FunctionName(int x, int y) //Two parameters in the function
{
return (x + y); //Returns the sum of the two numbers
}
// This function takes three integer parameters
public int FunctionName(int x, int y, int z)
{
return (x + y + z);
}
// This function takes two double parameters
public double FunctionName(double x, double y)
{
return (x + y);
}//Many more such methods can be done with different number of parameters
// Code used to input the number and
public static void main(String args[])
{

Object Oriented Programming -I


S. S. AGRAWAL INSTITUTE OF ENGINEERING & TECHNOLOGY, NAVSARI
DEPARTMENT OF COMPUTER ENGINEERING

FunctionName s = new FunctionName();


System.out.println(s.FunctionName(10, 20));
System.out.println(s. FunctionName(10, 20, 30));
System.out.println(s. FunctionName(10.5, 20.5));
}
}

3) Explain access modifiers with Example.

 There are two types of modifiers in Java: access modifiers and non-access modifiers.
 The access modifiers in Java specifies the accessibility or scope of a field, method, constructor,
or class. We can change the access level of fields, constructors, methods, and class by applying
the access modifier on it.

There are four types of Java access modifiers:

1. Private: The access level of a private modifier is only within the class. It cannot be accessed
from outside the class.
2. Default: The access level of a default modifier is only within the package. It cannot be
accessed from outside the package. If you do not specify any access level, it will be the
default.
3. Protected: The access level of a protected modifier is within the package and outside the
package through child class. If you do not make the child class, it cannot be accessed from
outside the package.
4. Public: The access level of a public modifier is everywhere. It can be accessed from within
the class, outside the class, within the package and outside the package.

There are many non-access modifiers, such as static, abstract, synchronized, native, volatile,
transient, etc. Here, we are going to learn the access modifiers only.

Object Oriented Programming -I


S. S. AGRAWAL INSTITUTE OF ENGINEERING & TECHNOLOGY, NAVSARI
DEPARTMENT OF COMPUTER ENGINEERING

Access within within outside package by subclass outside


Modifier class package only package

Private Y N N N

Default Y Y N N

Protected Y Y Y N

Public Y Y Y Y

4) Explain inheritance with its types and give suitable example

 Inheritance in Java is a mechanism in which one object acquires all the properties and
behaviors of a parent object. It is an important part of OOPs (Object Oriented programming
system).
 The idea behind inheritance in Java is that you can create new classes that are built upon
existing classes. When you inherit from an existing class, you can reuse methods and fields of
the parent class. Moreover, you can add new methods and fields in your current class also.
 Inheritance represents the IS-A relationship which is also known as a parent-
child relationship.

 Why use inheritance in java


o For Method Overriding (so runtime polymorphism can be achieved).
o For Code Reusability.

Types of inheritance in java

 On the basis of class, there can be three types of inheritance in java: single, multilevel and
hierarchical.
 In java programming, multiple and hybrid inheritance is supported through interface only.

Object Oriented Programming -I


S. S. AGRAWAL INSTITUTE OF ENGINEERING & TECHNOLOGY, NAVSARI
DEPARTMENT OF COMPUTER ENGINEERING

1) Single Inheritance Example

When a class inherits another class, it is known as a single inheritance. In the example given
below, Dog class inherits the Animal class, so there is the single inheritance.

class Animal{

void eat(){System.out.println("eating...");}

class Dog extends Animal{

void bark(){System.out.println("barking...");}

class TestInheritance{

public static void main(String args[]){

Dog d=new Dog();

d.bark();

Object Oriented Programming -I


S. S. AGRAWAL INSTITUTE OF ENGINEERING & TECHNOLOGY, NAVSARI
DEPARTMENT OF COMPUTER ENGINEERING

d.eat();

}}

2) Multilevel Inheritance Example

When there is a chain of inheritance, it is known as multilevel inheritance. As you can see in
the example given below, BabyDog class inherits the Dog class which again inherits the
Animal class, so there is a multilevel inheritance.

class Animal{

void eat(){System.out.println("eating...");}

class Dog extends Animal{

void bark(){System.out.println("barking...");}

class BabyDog extends Dog{

void weep(){System.out.println("weeping...");}

class TestInheritance2{

public static void main(String args[]){

BabyDog d=new BabyDog();

d.weep();

d.bark();

d.eat();

}}

Object Oriented Programming -I


S. S. AGRAWAL INSTITUTE OF ENGINEERING & TECHNOLOGY, NAVSARI
DEPARTMENT OF COMPUTER ENGINEERING

3) Hierarchical Inheritance

When two or more classes inherits a single class, it is known as hierarchical inheritance. In
the example given below, Dog and Cat classes inherits the Animal class, so there is hierarchical
inheritance.

class Animal{

void eat(){System.out.println("eating...");}

class Dog extends Animal{

void bark(){System.out.println("barking...");}

class Cat extends Animal{

void meow(){System.out.println("meowing...");}

class TestInheritance3{

public static void main(String args[]){

Cat c=new Cat();

c.meow();

c.eat();

//c.bark();//C.T.Error

}}

Object Oriented Programming -I


S. S. AGRAWAL INSTITUTE OF ENGINEERING & TECHNOLOGY, NAVSARI
DEPARTMENT OF COMPUTER ENGINEERING

5) Write difference between String class and StringBuffer class.

No. String StringBuffer

1) The String class is immutable. The StringBuffer class is mutable.

2) String is slow and consumes more StringBuffer is fast and consumes less memory
memory when we concatenate too many when we concatenate t strings.
strings because every time it creates new
instance.

3) String class overrides the equals() StringBuffer class doesn't override the equals()
method of Object class. So you can method of Object class.
compare the contents of two strings by
equals() method.

4) String class is slower while performing StringBuffer class is faster while performing
concatenation operation. concatenation operation.

5) String class uses String constant pool. StringBuffer uses Heap memory

6) Explain the words super, static and final with the help of an example.

Static Keyword
 It can be applied to nested static class, variables, methods and block.

 It is not required to initialize the static variable when it is declared.


 This variable can be re-initialized.
 It can access the static members of the class only.
 It can be called only by other static methods.
 Objects of static class can’t be created.
 Static class can only contain static members.
 It is used to initialize the static variables.

Example

class GFG {

// Creating a static variable and


// initializing a custom value
static int x = 10;

Object Oriented Programming -I


S. S. AGRAWAL INSTITUTE OF ENGINEERING & TECHNOLOGY, NAVSARI
DEPARTMENT OF COMPUTER ENGINEERING

// Creating a instance variable and


// initializing a custom value
int y = 20;

// Main driver method


public static void main(String[] args)
{

// Creating an object of class inside main() method


GFG t1 = new GFG();

// Accessing and re-initializing the


// static and instance variable
// using t1 reference
t1.x = 88;
t1.y = 99;

// Creating an object of class inside main() method


// again
GFG t2 = new GFG();
System.out.println("Value of Static variable x = " + t2.x + "\n"+ "Value of Instance
variable y = " + t2.y);
}
}

Final Keyword
 It is a keyword.
 It is used to apply restrictions on classes, methods and variables.
 It can’t be inherited.
 It can’t be overridden.
 Final methods can’t be inherited by any class.
 It is needed to initialize the final variable when it is being declared.
 Its value, once declared, can’t be changed or re-initialized.

Example

// Class 1
// Super-class
class P {

Object Oriented Programming -I


S. S. AGRAWAL INSTITUTE OF ENGINEERING & TECHNOLOGY, NAVSARI
DEPARTMENT OF COMPUTER ENGINEERING

// Method 1
// To declare first name
public void firstName()
{

// Passing name and print it


System.out.println("Mayank");
}

// Method 2
// To declare last name
public final void surName()
{
System.out.println("Trivedi");
}
}

class C extends P {

public void surName()


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

public static void main(String[] args)


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

Super
 The super keyword in Java is a reference variable which is used to refer immediate parent

class object.
 Whenever you create the instance of subclass, an instance of parent class is created
implicitly which is referred by super reference variable.
Usage of Java super Keyword
 super can be used to refer immediate parent class instance variable.
 super can be used to invoke immediate parent class method.
 super() can be used to invoke immediate parent class constructor.

Object Oriented Programming -I


S. S. AGRAWAL INSTITUTE OF ENGINEERING & TECHNOLOGY, NAVSARI
DEPARTMENT OF COMPUTER ENGINEERING

Example

class Animal{
String color="white";
}
class Dog extends Animal{
String color="black";
void printColor(){
System.out.println(color);//prints color of Dog class
System.out.println(super.color);//prints color of Animal class
}
}
class TestSuper1{
public static void main(String args[]){
Dog d=new Dog();
d.printColor();
}}

7) Explain abstract class with example.

 A class which is declared with the abstract keyword is known as an abstract class in Java. It
can have abstract and non-abstract methods (method with the body).
 Before learning the Java abstract class, let's understand the abstraction in Java first.
 Abstraction lets you focus on what the object does instead of how it does it.

Ways to achieve Abstraction

There are two ways to achieve abstraction in java

 Abstract class (0 to 100%)


 Interface (100%)

Abstract class in Java

 A class which is declared as abstract is known as an abstract class. It can have abstract
and non-abstract methods. It needs to be extended and its method implemented. It
cannot be instantiated.

Object Oriented Programming -I


S. S. AGRAWAL INSTITUTE OF ENGINEERING & TECHNOLOGY, NAVSARI
DEPARTMENT OF COMPUTER ENGINEERING

 An abstract class must be declared with an abstract keyword.


 It can have abstract and non-abstract methods.
 It cannot be instantiated.
 It can have constructors and static methods also.
 It can have final methods which will force the subclass not to change the body of the
method.

Example of abstract class

abstract class A{}

Abstract Method in Java

A method which is declared as abstract and does not have implementation is known as an
abstract method.

Example of abstract method

abstract void printStatus();//no method body and abstract

Example of Abstract class that has an abstract method

In this example, Bike is an abstract class that contains only one abstract method run. Its
implementation is provided by the Honda class.

abstract class Bike{


abstract void run();
}
class Honda4 extends Bike{
void run(){System.out.println("running safely");}
public static void main(String args[]){
Bike obj = new Honda4();
obj.run();
}
}

Object Oriented Programming -I


S. S. AGRAWAL INSTITUTE OF ENGINEERING & TECHNOLOGY, NAVSARI
DEPARTMENT OF COMPUTER ENGINEERING

8) List out different methods available for String class in java and explain any two with
proper example

 The java.lang.String class provides a lot of built-in methods that are used to
manipulate string in Java. By the help of these methods, we can perform operations on String
objects such as trimming, concatenating, converting, comparing, replacing strings etc.
 Java String is a powerful concept because everything is treated as a String if you submit any
form in window based, web based or mobile application.
 Let's use some important methods of String class.

 Java String toUpperCase() and toLowerCase() method

 The Java String toUpperCase() method converts this String into uppercase letter and String
toLowerCase() method into lowercase letter

Example

public class Stringoperation1

public static void main(String ar[])

String s="Sachin";

System.out.println(s.toUpperCase());//SACHIN

System.out.println(s.toLowerCase());//sachin

System.out.println(s);//Sachin(no change in original)

Object Oriented Programming -I


S. S. AGRAWAL INSTITUTE OF ENGINEERING & TECHNOLOGY, NAVSARI
DEPARTMENT OF COMPUTER ENGINEERING

9) What is an Exception? Explain the exception hierarchy. Explain how to throw, catch and
handle Exceptions

 The Exception Handling in Java is one of the powerful mechanism to handle the runtime
errors so that the normal flow of the application can be maintained.

Hierarchy of Java Exception classes

 The java.lang.Throwable class is the root class of Java Exception hierarchy inherited by two
subclasses: Exception and Error. The hierarchy of Java Exception classes is given below:

Object Oriented Programming -I


S. S. AGRAWAL INSTITUTE OF ENGINEERING & TECHNOLOGY, NAVSARI
DEPARTMENT OF COMPUTER ENGINEERING

Java Exception Keywords

Java provides five keywords that are used to handle the exception. The following table describes
each.

Keyword Description

try The "try" keyword is used to specify a block where we should place an exception code.
It means we can't use try block alone. The try block must be followed by either catch
or finally.

catch The "catch" block is used to handle the exception. It must be preceded by try block
which means we can't use catch block alone. It can be followed by finally block later.

finally The "finally" block is used to execute the necessary code of the program. It is executed
whether an exception is handled or not.

throw The "throw" keyword is used to throw an exception.

throws The "throws" keyword is used to declare exceptions. It specifies that there may occur
an exception in the method. It doesn't throw an exception. It is always used with
method signature.

Java Exception Handling Example

public class JavaExceptionExample{


public static void main(String args[]){
try{
//code that may raise exception
int data=100/0;
}catch(ArithmeticException e){System.out.println(e);}
//rest code of the program
System.out.println("rest of the code...");
}
}

Object Oriented Programming -I


S. S. AGRAWAL INSTITUTE OF ENGINEERING & TECHNOLOGY, NAVSARI
DEPARTMENT OF COMPUTER ENGINEERING

10) Explain the interface with an example program.


 An interface in Java is a blueprint of a class. It has static constants and abstract methods.
 The interface in Java is a mechanism to achieve abstraction. There can be only abstract
methods in the Java interface, not method body. It is used to achieve abstraction and
multiple inheritance in Java.
 In other words, you can say that interfaces can have abstract methods and variables. It
cannot have a method body.
 Java Interface also represents the IS-A relationship.
Why use Java interface?
There are mainly three reasons to use interface. They are given below.
 It is used to achieve abstraction.
 By interface, we can support the functionality of multiple inheritance.
 It can be used to achieve loose coupling.
Java Interface Example
In this example, the Printable interface has only one method, and its implementation is
provided in the A6 class.

interface printable{
void print();
}
class A6 implements printable{
public void print(){System.out.println("Hello");}

public static void main(String args[]){


A6 obj = new A6();
obj.print();
}
}

Object Oriented Programming -I


S. S. AGRAWAL INSTITUTE OF ENGINEERING & TECHNOLOGY, NAVSARI
DEPARTMENT OF COMPUTER ENGINEERING

11) Enlist various layout panes and explain any two in detail.

 Layouts are the top level container classes that define the UI styles for scene graph objects.
Layout can be seen as the parent node to all the other nodes. JavaFX provides various layout
panes that support different styles of layouts.
 In JavaFX, Layout defines the way in which the components are to be seen on the stage. It
basically organizes the scene-graph nodes. We have several built-in layout panes in JavaFX
that are HBox, VBox, StackPane, FlowBox, AnchorPane, etc. Each Built-in layout is
represented by a separate class which needs to be instantiated in order to implement that
particular layout pane.
 All these classes belong to javafx.scene.layout package. javafx.scene.layout.Pane class
is the base class for all the built-in layout classes in JavaFX.

Layout Classes

 javafx.scene.layout Package provides various classes that represents the layouts. The
classes are described in the table below.

Class Description

BorderPane Organizes nodes in top, left, right, centre and the bottom of the screen.

FlowPane Organizes the nodes in the horizontal rows according to the available horizontal
spaces. Wraps the nodes to the next line if the horizontal space is less than the total
width of the nodes

GridPane Organizes the nodes in the form of rows and columns.

HBox Organizes the nodes in a single row.

Pane It is the base class for all the layout classes.

StackPane Organizes nodes in the form of a stack i.e. one onto another

VBox Organizes nodes in a vertical column.

Object Oriented Programming -I


S. S. AGRAWAL INSTITUTE OF ENGINEERING & TECHNOLOGY, NAVSARI
DEPARTMENT OF COMPUTER ENGINEERING

JavaFX BorderPane

 BorderPane arranges the nodes at the left, right, centre, top and bottom of the screen. It is
represented by javafx.scene.layout.BorderPane class. This class provides various
methods like setRight(), setLeft(), setCenter(), setBottom() and setTop() which are
used to set the position for the specified nodes. We need to instantiate BorderPane class to
create the BorderPane layout.
 The properties of BorderPane class along with their setter methods are given in the table
below.

Type Property Setter Methods Description

Node Bottom setBottom() Add the node to the bottom of the screen

Node Centre setCentre() Add the node to the centre of the screen

Node Left setLeft() Add the node to the left of the screen

Node Right setRight() Add the node to the right of the screen

Node Top setTop() Add the node to the top of the screen

Example:

package application;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.layout.*;
import javafx.stage.Stage;
public class Label_Test extends Application {

@Override
public void start(Stage primaryStage) throws Exception {
BorderPane BPane = new BorderPane();
BPane.setTop(new Label("This will be at the top"));
BPane.setLeft(new Label("This will be at the left"));

Object Oriented Programming -I


S. S. AGRAWAL INSTITUTE OF ENGINEERING & TECHNOLOGY, NAVSARI
DEPARTMENT OF COMPUTER ENGINEERING

BPane.setRight(new Label("This will be at the Right"));


BPane.setCenter(new Label("This will be at the Centre"));
BPane.setBottom(new Label("This will be at the bottom"));
Scene scene = new Scene(BPane,600,400);
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}

JavaFX HBox

 HBox layout pane arranges the nodes in a single row. It is represented by


javafx.scene.layout.HBox class. We just need to instantiate HBox class in order to
create HBox layout.

Example:

package application;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;
public class Label_Test extends Application {

@Override
public void start(Stage primaryStage) throws Exception {
Button btn1 = new Button("Button 1");
Button btn2 = new Button("Button 2");

Object Oriented Programming -I


S. S. AGRAWAL INSTITUTE OF ENGINEERING & TECHNOLOGY, NAVSARI
DEPARTMENT OF COMPUTER ENGINEERING

HBox root = new HBox();


Scene scene = new Scene(root,200,200);
root.getChildren().addAll(btn1,btn2);
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}

12) Explain Thread Synchronization with example.

 Synchronization in Java is the capability to control the access of multiple threads to any shared
resource.
 Java Synchronization is better option where we want to allow only one thread to access the
shared resource.

Why use Synchronization?

The synchronization is mainly used to

1. To prevent thread interference.


2. To prevent consistency problem.

Types of Synchronization

There are two types of synchronization

Process Synchronization
Thread Synchronization

Object Oriented Programming -I


S. S. AGRAWAL INSTITUTE OF ENGINEERING & TECHNOLOGY, NAVSARI
DEPARTMENT OF COMPUTER ENGINEERING

Thread Synchronization
There are two types of thread synchronization mutual exclusive and inter-thread
communication.

I. Mutual Exclusive
 Synchronized method.
 Synchronized block.
II. Static synchronization.
 Cooperation (Inter-thread communication in java)
 Mutual Exclusive

Java Synchronized Method


 If you declare any method as synchronized, it is known as synchronized method.

 Synchronized method is used to lock an object for any shared resource.

 When a thread invokes a synchronized method, it automatically acquires the lock for
that object and releases it when the thread completes its task.

TestSynchronization2.java

//example of java synchronized method


class Table{
synchronized void printTable(int n){//synchronized method
for(int i=1;i<=5;i++){
System.out.println(n*i);
try{
Thread.sleep(400);
}catch(Exception e){System.out.println(e);}
}

}
}

Object Oriented Programming -I


S. S. AGRAWAL INSTITUTE OF ENGINEERING & TECHNOLOGY, NAVSARI
DEPARTMENT OF COMPUTER ENGINEERING

class MyThread1 extends Thread{


Table t;
MyThread1(Table t){
this.t=t;
}
public void run(){
t.printTable(5);
}

}
class MyThread2 extends Thread{
Table t;
MyThread2(Table t){
this.t=t;
}
public void run(){
t.printTable(100);
}
}

public class TestSynchronization2{


public static void main(String args[]){
Table obj = new Table();//only one object
MyThread1 t1=new MyThread1(obj);
MyThread2 t2=new MyThread2(obj);
t1.start();
t2.start();
}
}

Object Oriented Programming -I


Reference Book
S. S. AGRAWAL INSTITUTE OF ENGINEERING & TECHNOLOGY, NAVSARI
DEPARTMENT OF COMPUTER ENGINEERING

Subject: 3140705 (Object Oriented Programming -I)


Reference Books

Intro to Java Programming, 10th edition, Y.Daniel Liang, Pearson


1

Object oriented programming with Java , Rajkumar Buyya,S Thamarai Selvi,


Xingchen Chu, McGrawHill
2

Programming in Java, Sachin Malhotra, Saurabh Choudhary, Oxford


3

Programming with JAVA , E Balagurusamy, McGrawHill


4

CORE JAVA volume -I Cay Horstmann, Pearson


5

Object Oriented Programming -I

You might also like