Java & Computer Graphics Viva Questions and Answers
Expt. 1 – Java Calculator (Input, Conditionals, Loops, Exception Handling)
Aim: Implement a calculator using conditional logic, loops, and exception
handling.
OOP concepts used : Classes, Objects, Encapsulation, Exception Handling
Q: What is Java?
A: Java is an object-oriented, platform-independent programming language.
Q: What is the use of Scanner class?
A: It is used to take input from the user at runtime.
Q: What is a variable?
A: A variable is a named memory location used to store data.
Q: What is a data type?
A: It specifies the type and size of data a variable can hold.
Q: What is an operator?
A: An operator is a symbol that performs operations on operands.
Q: What is the difference between = and ==?
A: `=` is used for assignment; `==` is used for comparison.
Q: What is the use of if statement?
A: It executes a block of code only when a condition is true.
Q: What is a loop?
A: A loop executes a block of code repeatedly.
Q: What is an error?
A: An error is a problem that stops normal execution of the program.
Q: What does the try-catch block do?
A: It handles exceptions and prevents program crashes.
Q: Which package must be imported to use Scanner?
A: `java.util.*`
Q: What is the difference between if-else and switch?
A: `if-else` tests boolean conditions; `switch` tests a single expression against many cases.
Q: What happens if you divide a number by zero in Java?
A: It throws an ArithmeticException.
Q: Why is exception handling important?
A: It ensures program stability and handles unexpected errors gracefully.
Q: What does the finally block do?
A: It executes code whether or not an exception occurs.
Q: Can we nest loops in Java?
A: Yes, one loop can exist inside another.
Q: Which loop is best when number of iterations is known?
A: The for loop.
Q: What data types are commonly used in a calculator?
A: int, double, or float.
Expt. 2 – Single Inheritance
Aim: Demonstrate single inheritance where a subclass extends a superclass.
OOP concepts used : Inheritance, Method Overriding
Q: What is inheritance?
A: It is a mechanism by which one class acquires properties of another.
Q: What keyword is used for inheritance?
A: `extends`
Q: What is a superclass?
A: A parent class whose members are inherited.
Q: What is a subclass?
A: A child class that inherits from a superclass.
Q: Can a subclass use methods of its parent class?
A: Yes, except private members.
Q: What is the use of super keyword?
A: It is used to call parent class methods or constructors.
Q: Can a class have more than one parent class in Java?
A: No, Java does not support multiple inheritance through classes.
Q: Why is inheritance used?
A: To promote code reuse and reduce redundancy.
Q: What is method overriding?
A: Redefining a method of the parent class in the child class.
Q: What is the main benefit of inheritance?
A: Code reusability.
Q: What is the syntax for inheritance in Java?
A: class Subclass extends Superclass { }
Q: What is the use of the super keyword?
A: To access superclass variables, methods, or constructors.
Expt. 3 – Multiple Inheritance using Interfaces
Aim: Demonstrate polymorphism using multiple interfaces.
OOP concepts used : Interfaces, Multiple Inheritance, Polymorphism,
Abstraction
Q: What is an interface?
A: An interface is a collection of abstract methods (methods without a body) that other
classes can implement.
Eg:
interface Animal {
void eat();
void sleep();
Here, Animal is an interface.
It says that any class that implements it must have eat() and sleep() methods.
Q: Which keyword is used to implement an interface?
A: `implements`
Q: Can a class implement more than one interface?
A: Yes.
Q: Can an interface have method bodies?
A: No, except default or static methods (from Java 8 onward).
Q: Can we create an object of an interface?
A: No, because interfaces are abstract.
Q: What keyword is used to inherit one interface from another?
A: `extends`
Q: Why do we use interfaces?
A: To achieve abstraction and multiple inheritance.
Q: Does Java support multiple inheritance through classes?
A: No, only through interfaces.
Q: What is polymorphism?
A: The ability of one method to behave differently based on the object calling it.
Q: What is the default access modifier of interface methods?
A: Public and abstract by default.
Expt. 4 – ATM Simulation with Exception Handling
Aim: ATM functionalities with exception handling and classes.
OOP concepts used : Encapsulation, Abstraction, Inheritance, Polymorphism,
Exception Handling
Q: What is an exception?
A: An error that occurs during program execution.
Q: What is the use of exception handling?
A: To manage errors gracefully without stopping execution.
Q: What keywords are used for exception handling?
A: `try`, `catch`, `finally`, `throw`, `throws`.
Q. What is a try block?
A. A block that contains code which might throw an exception.
Q. What is a catch block?
A. A block that handles the exception.
Q. What is a finally block?
A. A block that always executes after try-catch.
Q. What is Arithmetic Exception?
A. An exception caused by illegal arithmetic operations like divide by zero.
Q. Why do we use throw and throws?
A. throw is used to manually throw an exception; throws declares it.
Q. Can we have multiple catch blocks?
A. Yes, to handle different types of exceptions.
Q. Why is finally used?
A. To execute cleanup code regardless of exception.
Q: Which exceptions can occur in an ATM system?
A: ArithmeticException, IllegalArgumentException, or custom exceptions like
InsufficientFundsException.
Q: What are the main OOP concepts used in this program?
A: Encapsulation, abstraction, and exception handling.
Q: Why use custom exceptions?
A: To handle domain-specific errors more clearly.
Expt. 5 – Multithreading
Aim: Create a multithreaded Java application simulating real-time operations.
OOP concepts used : Classes, Objects, Inheritance, Polymorphism (Threads)
Q: What is a thread?
A: A smallest unit of execution in a program.
Q: How do we create a thread in Java?
A: By extending Thread class or implementing Runnable.
Q: What is the difference between start() and run()?
A: `start()` creates a new thread and calls `run()`; `run()` executes in the same thread.
Q4. What is the use of join()?
A. It makes one thread wait until another thread finishes.
Q5. What does isAlive() return?
A. True if thread is still running; false otherwise.
Q6. How to set thread priority?
A. Using setPriority() method.
Q7. What is synchronization?
A. Mechanism to control access to shared resources.
Q8. Can we stop a thread forcibly?
A. Not safely; stop() is deprecated.
Q9. What are the different states of a thread?
A. New, Runnable, Running, Waiting, Terminated.
Q10. What is the advantage of using threads?
A. Better performance through parallel execution.
📈 Expt. 6 – Line Drawing Algorithms
Aim: Draw lines using two algorithms.
OOP concepts used : Encapsulation, Abstraction
Q1. What does DDA stand for?
A. Digital Differential Analyzer.
DDA Algorithm Steps:
1. Input the two end points —
(x 1 , y 1 )and (x 2 , y 2 )
2. Calculate the differences:
dx=x 2−x 1 , dy= y 2− y1
3. Find the number of steps:
steps=max(∣ dx ∣ ,∣ dy ∣)
4. Calculate increments:
dx dy
x inc= ,y =
steps inc steps
5. Set starting point:
x=x 1 , y = y 1
6. Plot the first point (x , y )
7. Repeat for all steps:
o x=x + x inc
o y= y+ y inc
o Plot the new point
8. Stop when the end point (x 2 , y 2 )is reached.
Q2. What is the main idea of DDA?
A. It increments x and y step-by-step to plot a line.
Q3. What is Bresenham’s algorithm?
A. A fast integer-based line drawing algorithm.
Bresenham’s Algorithm Steps
1. Input the two end points of the line —
(x 1 , y 1 )and (x 2 , y 2 )
2. Calculate the differences:
dx=x 2−x 1 , dy= y 2− y1
3. Find the decision parameter:
p=2dy −dx
4. Set starting point:
x=x 1 , y = y 1
5. Plot the first point (x , y )
6. For each x from x₁ to x₂:
o If p < 0,
→ next point is (x +1 , y )
→ update p= p+ 2dy
o Else
→ next point is (x +1 , y +1)
→ update p= p+ 2(dy−dx)
7. Repeat until the end point (x 2 , y 2 )is reached.
Q4. Which is faster — DDA or Bresenham’s?
A. Bresenham’s algorithm.
Q: What is the difference between DDA and Bresenham’s algorithm?
A: DDA uses floating-point calculations; Bresenham uses only integer arithmetic,
making it faster.
Q5. What is the slope of a line?
A. The ratio of change in y to change in x (Δy/Δx).
Q6. What are dotted and solid lines?
A. Dotted – with gaps; Solid – continuous.
Q7. When is DDA less accurate?
A. When floating-point round-off errors occur.
Q8. Why do we round off values in DDA?
A. To map real values to integer pixel positions.
Q9. What are integer calculations used in Bresenham’s?
A. Decision parameters based on integers only.
Q10. Can we draw a vertical line using DDA?
A. Yes.
⚪ Expt. 7 – Circle Drawing Algorithms
Aim: Draw circles using DDA, Bresenham’s, and Midpoint algorithms.
OOP concepts used : Encapsulation, Abstraction, Polymorphism
Q1. What is a circle?
A. A set of points at equal distance from a center.
Q2. What is the Midpoint Circle Algorithm?
A. An algorithm to plot a circle using integer arithmetic.
Steps of Midpoint Circle Algorithm:
1. Input the radius r and the center point (xc , yc)
2. Start at the top of the circle:
x=0 , y=r
3. Calculate the initial decision parameter:
p=1−r
4. Plot the first point:
Plot the point (x + xc , y + yc )
(and all other 7 symmetric points — because a circle is symmetrical)
5. Repeat while x < y :
o If p<0 :
→ Next point is (x +1 , y )
→ Update p= p+ 2 x +3
o Else:
→ Next point is (x +1 , y −1)
→ Update p= p+ 2 x−2 y +5
o Plot all 8 symmetric points of the circle.
6. Stop when x ≥ y
Q: What is the principle of the midpoint circle algorithm?
A: It uses decision parameters to select pixels closest to the ideal circle.
Q: What is the advantage of midpoint algorithm?
A: It avoids floating-point operations.
Q3. Why is a circle symmetric?
A. All points are equidistant from the center.
Q4. What is the advantage of Bresenham’s circle algorithm?
A. Uses only integer operations — fast and accurate.
Q5. Why do we use symmetry in circle drawing?
A. To reduce computations by plotting one octant and mirroring.
Q6. What parameters are required to draw a circle?
A. Center coordinates (x, y) and radius r.
Q7. What happens when we change the radius?
A. Circle size changes.
Q8. Which algorithm uses only integer arithmetic?
A. Bresenham’s circle algorithm.
Q9. Can we draw a circle without trigonometric functions?
A. Yes, using Midpoint or Bresenham’s algorithm.
Q10. What is the output of the circle drawing algorithm?
A. A set of pixels forming a circle on screen.
🧭 Expt. 8 – 2D Transformations (Translation, Rotation, Shear, Scaling)
Aim: Perform geometric transformations on 2D objects.
OOP concepts used : Encapsulation, Abstraction, Polymorphism
Q1. What is translation?
A. Moving an object from one position to another.
Q2. What is scaling?
A. Changing the size of an object.
Q3. What is shear?
A. Slanting the shape of an object.
Q4. What is rotation?
A. Turning an object around a point or axis.
Q5. Which matrix is used for translation?
A. Translation matrix [10tx ],[01ty ],[001]
Q6. What is the effect of scaling factor < 1?
A. Object shrinks.
Q7. What is the difference between uniform and non-uniform scaling?
A. Uniform – same factor on x & y; Non-uniform – different factors.
Q8. About which axis do we rotate the object?
A. Usually about the origin.
Q9. What happens if rotation angle is negative?
A. Object rotates clockwise.
Q10. Why do we use transformations in graphics?
A. To move, resize, and rotate objects on screen.
Q11: What are homogeneous coordinates?
A: 3D coordinates used to represent 2D transformations using matrix multiplication.
🔺 Expt. 9 – Transformations on Triangle and Rhombus
Aim: Perform transformations like rotation and scaling on specific shapes.
OOP concepts used : Inheritance, Polymorphism, Abstraction
Q1. What is a triangle?
A. A polygon with three sides.
Q2. What is a rhombus?
A. A quadrilateral with four equal sides.
Q3. Difference between square and rhombus?
A. Square has right angles; rhombus may not.
Q4. Which transformation changes size?
A. Scaling.
Q5. Which transformation changes position?
A. Translation.
Q6. Which transformation changes shape but not size?
A. Shear.
Q7. Can we rotate a triangle?
A. Yes.
Q8. Can shear make a square into a rhombus?
A. Yes.
Q9. Why is matrix multiplication used in transformations?
A. To combine multiple transformations efficiently.
Q10. Can translation change shape?
A. No, only position.
Q11: What happens to an object when rotated b y 180°?
A: It appears inverted around the rotation point.
Q12: Why use transformation matrices?
A: They make applying multiple transformations easier using matrix multiplication.
🔄 Expt. 10 – Rotation about Arbitrary Point
Aim: Implement rotation about X-axis and arbitrary point.
OOP concepts used : Encapsulation, Abstraction, Inheritance
Q1. What is an arbitrary point?
A. Any point (x, y) about which rotation is performed.
Q2. What is rotation about origin?
A. Rotating an object with respect to (0,0).
Q3. Why do we rotate about a point?
A. To control the pivot of rotation.
Q4. What is the formula for rotation?
A. x' = xcosθ – ysinθ; y' = xsinθ + ycosθ.
Q5. Why do we translate before rotation?
A. To move the arbitrary point to the origin.
Q6. How do we bring it back after rotation?
A. By translating back to original position.
Q7. What happens if rotation angle = 0°?
A. No change in object position.
Q8. What is the unit of rotation angle?
A. Degrees or radians.
Q9. Can we rotate clockwise?
A. Yes, by using a negative angle.
Q10. What is the use of rotation transformation?
A. To orient or animate objects in graphics.
Q11: What is composite transformation?
A: A combination of two or more transformations (e.g., translation + rotation).
11. Banking System
Aim: Implement OOP concepts in a banking system.
OOP concepts used : Encapsulation, Inheritance, Polymorphism, Abstraction,
Classes, Objects
Viva Questions:
1. Q: What OOP principles are used here?
A: Inheritance, encapsulation, abstraction, and polymorphism.
2. Q: What is data hiding?
A: Restricting access to internal data using private variables and getters/setters.
3. Q: How can you implement a class for account details?
A: Using attributes like account number, name, balance, and methods like deposit,
withdraw.
4. Q: What is a constructor?
A: A special method used to initialize objects.
5. Q: What is pixel?
A: A pixel is the smallest controllable element of a digital image or display that
represents a single point of color in a graphic.
6. Q: What is resolution?
A: Resolution is the measure of how many pixels are displayed on the screen,
usually given as the number of pixels in width × height.
7. Q: What is the syntax of object declaration?
A: ClassName objectName = new ClassName();
Eg: Car myCar = new Car();
SYNTAXS :
Java Viva Syntax Sheet
1. Class Declaration
class ClassName {
// data members
// methods
}
2. Object Declaration and Creation
ClassName object_name = new ClassName();
3. Constructor Declaration
ClassName() {
// initialization code
}
4. Method Declaration
returnType methodName(parameterList) {
// method body
}
5. Method Calling
objectName.methodName(arguments);
6. Inheritance
class SubclassName extends SuperclassName {
// additional members
}
7. Using super Keyword
super.variableName;
super.methodName();
super();
8. Interface Declaration and Implementation
interface InterfaceName {
void method1();
}
class ClassName implements InterfaceName {
public void method1() {
// implementation
}
}
9. Abstract Class
abstract class ClassName {
abstract void display();
}
class SubClass extends ClassName {
void display() {
System.out.println("Abstract method implemented");
}
}
10. Method Overloading
void add(int a, int b) { }
void add(double a, double b) { }
11. Method Overriding
class A {
void show() { }
}
class B extends A {
void show() { } // overriding
}
12. Exception Handling Syntax
try {
// code that may throw exception
} catch(ExceptionType e) {
// handling code
} finally {
// cleanup code
}
13. Thread Creation
// By extending Thread class
class MyThread extends Thread {
public void run() {
System.out.println("Thread running");
}
}
MyThread t1 = new MyThread();
t1.start();
// By implementing Runnable interface
class MyThread implements Runnable {
public void run() {
System.out.println("Thread running");
}
}
Thread t1 = new Thread(new MyThread());
t1.start();
14. Scanner Class (User Input)
import java.util.Scanner;
Scanner sc = new Scanner(System.in);
int num = sc.nextInt();
String name = sc.nextLine();
15. Object Array Declaration
ClassName[] objArray = new ClassName[size];
16. Static Members
static int count;
static void display() { }
17. Encapsulation
private int id;
public void setId(int id) { this.id = id; }
public int getId() { return id; }
18. this Keyword
this.variableName = variableName;
19. final Keyword
final int MAX = 100;
final void show() { }
final class Test { }
20. Package Declaration
package myPackage;