Java Pyq
Java Pyq
Contents
1 Principles of Object-Oriented Programming (OOP) 5
1.1 Theory . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
1.2 Core OOP Principles . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
1.2.1 (1) Encapsulation . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
1.2.2 (2) Inheritance . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
1.2.3 (3) Polymorphism . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
1.2.4 (4) Abstraction . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
1.2.5 (5) Class and Object . . . . . . . . . . . . . . . . . . . . . . . . . 5
4 Applet vs Application 7
4.1 Comparison Table . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
4.2 Example Programs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
4.2.1 Simple Applet Code . . . . . . . . . . . . . . . . . . . . . . . . . 7
4.2.2 Simple Application Code (Console Program) . . . . . . . . . . . . 8
1
CSE Department Java Programming - PYQ Solutions
11 Recursion vs Iteration 15
11.1 Comparison Table . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
11.2 Example: Factorial using Recursion & Iteration . . . . . . . . . . . . . . 16
11.2.1 Recursion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
11.2.2 Iteration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
Page 2
CSE Department Java Programming - PYQ Solutions
16 Constructor Chaining 20
16.1 Can one constructor call another constructor? . . . . . . . . . . . . . . . 20
16.2 Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
16.3 Why Constructor Chaining? . . . . . . . . . . . . . . . . . . . . . . . . . 21
16.4 Rules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
24 Operator Precedence 28
24.1 Definition . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28
24.2 Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28
Page 3
CSE Department Java Programming - PYQ Solutions
26 Abstraction vs Polymorphism 29
26.1 Abstraction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29
26.2 Polymorphism . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29
29 Operators in Java 31
29.1 1. Arithmetic Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . 31
29.2 2. Relational Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31
29.3 3. Logical Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31
29.4 4. Assignment Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . 31
29.5 5. Increment/Decrement Operators . . . . . . . . . . . . . . . . . . . . . 31
29.6 6. Bitwise Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31
29.7 7. Ternary Operator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31
29.8 8. Instanceof Operator . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31
33 Exam Tips 35
Page 4
CSE Department Java Programming - PYQ Solutions
Page 5
CSE Department Java Programming - PYQ Solutions
Page 6
CSE Department Java Programming - PYQ Solutions
1 int i = 1;
2 while ( i <= 5) {
3 System . out . println ( i ) ;
4 i ++;
5 }
1 int i = 1;
2 do {
3 System . out . println ( i ) ;
4 i ++;
5 } while ( i <= 5) ;
4 Applet vs Application
4.1 Comparison Table
4.2 Example Programs
4.2.1 Simple Applet Code
Page 7
CSE Department Java Programming - PYQ Solutions
Applet Application
Runs inside a browser or applet Runs independently as a standalone
viewer program
Uses applet viewer Uses JVM (java command)
Cannot access system resources (for Can access system resources easily
security)
Needs an HTML page to run Does not need HTML
Strong security restrictions No such restrictions
Mostly GUI-based Can be GUI or Console
Uses init(), start(), paint() Uses main() method
HTML to run:
1 < applet code = " MyApplet . class " width = " 200 " height = " 100 " > </
applet >
1 class SimpleApp {
2 public static void main ( String [] args ) {
3 System . out . println ( " Hello Application ! " ) ;
4 }
5 }
Page 8
CSE Department Java Programming - PYQ Solutions
• No data loss
• Example: int → long → float → double
Example:
1 int a = 20;
2 double b = a ; // int automatically converted to double
Page 9
CSE Department Java Programming - PYQ Solutions
7 System . out . println ( " After Swap ( XOR ) : a = " + a + " , b = "
+ b);
8 }
9
7 System . out . println ( " After Swap ( Mul / Div ) : a = " + a + " ,
b=" + b);
Page 10
CSE Department Java Programming - PYQ Solutions
8 }
9
6 Frame f ;
7 Button b ;
8 Label l ;
9
10 ButtonEventDemo () {
11 f = new Frame ( " Event Handling Example " ) ;
12 b = new Button ( " Click Me " ) ;
13 l = new Label ( " Waiting for click ... " ) ;
14
18 f . add ( b ) ;
19 f . add ( l ) ;
20 f . setSize (300 , 300) ;
21 f . setLayout ( null ) ;
22 f . setVisible ( true ) ;
23
Page 11
CSE Department Java Programming - PYQ Solutions
• Added a label
4 public class A no ny mo us Ev en tD em o {
5
Page 12
CSE Department Java Programming - PYQ Solutions
15 f . add ( b ) ;
16 f . add ( l ) ;
17 f . setSize (300 , 300) ;
18 f . setLayout ( null ) ;
19 f . setVisible ( true ) ;
20
8.4.1 Explanation
• No class implements ActionListener
• We create an event handler inside the method itself
• It executes when the button is pressed
• Used when event is small and short
Page 13
CSE Department Java Programming - PYQ Solutions
6 // Writing to file
7 FileWriter fw = new FileWriter ( " demo . txt " ) ;
8 fw . write ( " Welcome to Java File Handling !\ nThis is a
sample text . " ) ;
9 fw . close () ;
10 System . out . println ( " Data written to file successfully .
");
11
Page 14
CSE Department Java Programming - PYQ Solutions
• If you try to modify a String, Java creates a new object instead of modifying the
existing one
Explanation:
• When “Hello World” is created, Java creates a new String object, proving immutability
11 Recursion vs Iteration
11.1 Comparison Table
Page 15
CSE Department Java Programming - PYQ Solutions
11.2.2 Iteration
14 class Test {
15 public static void main ( String [] args ) {
16 Dog d = new Dog () ;
17 }
18 }
Page 16
CSE Department Java Programming - PYQ Solutions
Output:
• So methods can modify object data, but not the reference itself
5 class PassObject {
6 static void change ( Box b ) {
7 b . length = 50; // modifies original object
8 }
9
Page 17
CSE Department Java Programming - PYQ Solutions
Throwable
/ \
Exception Error
/ \
Checked Ex. Unchecked Ex.
14.1.1 1. Throwable
• Superclass of all errors and exceptions
14.1.2 2. Exception
Represents conditions that a program should handle.
A. Checked Exceptions (Compile-time exceptions)
C. Errors
Page 18
CSE Department Java Programming - PYQ Solutions
Output:
Example:
1 class OverloadDemo {
2 int add ( int a , int b ) { return a + b ; }
3 double add ( double a , double b ) { return a + b ; }
4 }
• Same parameters
Example:
1 class Parent {
2 void show () { System . out . println ( " Parent class " ) ; }
3 }
4
Page 19
CSE Department Java Programming - PYQ Solutions
• Increases readability
Overriding
• Improves extensibility
16 Constructor Chaining
16.1 Can one constructor call another constructor?
YES — Constructor Chaining
Page 20
CSE Department Java Programming - PYQ Solutions
16.2 Example
1 class Student {
2 Student () {
3 this (100) ; // calling another constructor
4 System . out . println ( " Default constructor " ) ;
5 }
6
7 Student ( int id ) {
8 System . out . println ( " ID = " + id ) ;
9 }
10 }
• Cleaner code
• Easier maintenance
16.4 Rules
• this() must be FIRST statement in constructor
Table 5: = vs == operators
• = assigns a value
• == checks equality
6 if ( a == b ) // comparison
7 System . out . println ( " Both are equal " ) ;
Page 21
CSE Department Java Programming - PYQ Solutions
8 else
9 System . out . println ( " Not equal " ) ;
10 }
11 }
Page 22
CSE Department Java Programming - PYQ Solutions
• Average = total / 3
3 class StudentMarks {
4 public static void main ( String [] args ) {
5 Scanner sc = new Scanner ( System . in ) ;
6
16 int total = m1 + m2 + m3 ;
17 double avg = total / 3.0;
18
Page 23
CSE Department Java Programming - PYQ Solutions
21 }
22 }
10 Circle ( double r ) {
11 radius = r ;
12 }
13
14 @Override
15 void area () {
16 double a = 3.14 * radius * radius ;
17 System . out . println ( " Area of Circle = " + a ) ;
18 }
19 }
20
29 @Override
30 void area () {
Page 24
CSE Department Java Programming - PYQ Solutions
36 class ShapeTest {
37 public static void main ( String [] args ) {
38 Shape s ;
39
Output Sample:
Area of Circle = 78.5
Area of Rectangle = 24.0
• Helps in:
– Encapsulation
– Name-space management
– Code reusability
• Two steps:
Page 25
CSE Department Java Programming - PYQ Solutions
1 package library ;
2
3 class LibraryTest {
4 public static void main ( String [] args ) {
5 Book b1 = new Book ( " Java Programming " , " James Gosling "
, 450.00) ;
6 b1 . show () ;
7 }
8 }
javac library/[Link]
javac [Link]
java LibraryTest
Page 26
CSE Department Java Programming - PYQ Solutions
6 void display () {
7 synchronized ( this ) { // synchronized block
8 System . out . println ( " Only this block is locked " ) ;
9 }
10 }
11 }
3 class CurrentDateTime {
4 public static void main ( String [] args ) {
5 LocalDateTime now = LocalDateTime . now () ;
6 System . out . println ( " Current Date & Time : " + now ) ;
7 }
8 }
Page 27
CSE Department Java Programming - PYQ Solutions
24 Operator Precedence
24.1 Definition
Operator precedence means which operator is evaluated first when an expression has
multiple operators.
24.2 Example
1 int result = 10 + 20 * 3;
Explanation:
• So first: 20 * 3 = 60
• Then: 10 + 60 = 70
Output: 70
1 void show () {
2 int x = 10; // local variable
3 System . out . println ( x ) ;
4 }
1 class Demo {
2 int age = 20; // instance variable
3 }
Page 28
CSE Department Java Programming - PYQ Solutions
1 class Demo {
2 static int count = 0; // class variable
3 }
26 Abstraction vs Polymorphism
26.1 Abstraction
• Hides internal details
Example:
1 abstract class Animal {
2 abstract void sound () ;
3 }
26.2 Polymorphism
• Many forms of the same method
Example:
1 class Shape {
2 void draw () { System . out . println ( " Drawing shape " ) ; }
3 }
4
Page 29
CSE Department Java Programming - PYQ Solutions
3 class WriteFile {
4 public static void main ( String [] args ) throws Exception {
5 FileWriter fw = new FileWriter ( " data . txt " ) ;
6 fw . write ( " Hello Java ! " ) ;
7 fw . close () ;
8 }
9 }
28.2.2 Read:
3 class ReadFile {
4 public static void main ( String [] args ) throws Exception {
5 FileReader fr = new FileReader ( " data . txt " ) ;
6 int ch ;
7 while (( ch = fr . read () ) != -1) {
8 System . out . print (( char ) ch ) ;
9 }
10 fr . close () ;
11 }
12 }
Page 30
CSE Department Java Programming - PYQ Solutions
4 int i ;
5 while (( i = fin . read () ) != -1) {
6 fout . write ( i ) ;
7 }
8 fin . close () ;
9 fout . close () ;
29 Operators in Java
29.1 1. Arithmetic Operators
+ - * / %
Page 31
CSE Department Java Programming - PYQ Solutions
30.1.2 Example
1 class Outer {
2 static int x = 10;
3 int y = 20;
4
12 // Inner Class
13 class Inner {
14 void display () {
15 System . out . println ( " x = " + x + " , y = " + y ) ;
16 }
17 }
18 }
19
26 // Inner Class
27 Outer o = new Outer () ;
28 Outer . Inner in = o . new Inner () ;
29 in . display () ;
30 }
31 }
Page 32
CSE Department Java Programming - PYQ Solutions
30.2.2 Example
1 class Singleton {
2 private static Singleton instance ;
3
13 void show () {
14 System . out . println ( " Singleton instance " ) ;
15 }
16 }
17
23 s1 . show () ;
24 System . out . println ( s1 == s2 ) ; // true
25 }
26 }
30.3.2 Example
1 class Parent {
2 void message () { System . out . println ( " Parent class " ) ; }
Page 33
CSE Department Java Programming - PYQ Solutions
3 }
4
31.2 Program
1 import java . util .*;
2
3 class StringToList {
4 public static void main ( String [] args ) {
5 String str = " JavaProgramming " ;
6 List < Character > list = new ArrayList < >() ;
7
8 try {
9 for ( int i = 0; i < str . length () ; i ++) {
10 list . add ( str . charAt ( i ) ) ;
11 }
12 } catch ( Exception e ) {
13 System . out . println ( " Exception occurred : " + e ) ;
14 }
15
Output:
[J, a, v, a, P, r, o, g, r, a, m, m, i, n, g]
Page 34
CSE Department Java Programming - PYQ Solutions
32.2 Program
1 class MyThread extends Thread {
2 String name ;
3 MyThread ( String name ) { this . name = name ; }
4
18 t1 . start () ;
19 try { t1 . join () ; } catch ( Exception e ) {} // main
waits for t1
20
21 t2 . start () ;
22 try { t2 . join () ; } catch ( Exception e ) {} // main
waits for t2
23
Output:
Thread-1 running: 1
Thread-1 running: 2
...
Thread-2 running: 1
...
All threads finished
33 Exam Tips
1. Write theory first, then short code snippet
Page 35
CSE Department Java Programming - PYQ Solutions
Page 36