1.
Answer the Following Questions:
i) Difference between Logical Error and Syntax Error
● Syntax Error: These occur when the code does not conform to the rules of the
programming language. For example, missing a semicolon or using incorrect keywords.
Syntax errors are typically caught at compile-time.
● Logical Error: These occur when the code is syntactically correct but does not produce
the expected results due to a mistake in logic. For example, using an incorrect formula to
calculate a value. Logical errors are usually discovered during runtime.
ii) Significance of Using the Word ‘new’ in Object Creation
● The new keyword in Java is used to create a new instance of a class. When you use
new, you are allocating memory for the object and initializing it. For example, MyClass
obj = new MyClass(); creates a new instance of MyClass.
iii) Types of Arithmetical Expressions
● Unary Operators: Operate on a single operand. Examples: +, -, ++, --.
● Binary Operators: Operate on two operands. Examples: +, -, *, /, %.
● Ternary Operator: A conditional operator that takes three operands. Example:
condition ? expr1 : expr2.
iv) Principles of Object-Oriented Programming (OOP)
1. Encapsulation: Bundling the data (attributes) and methods (functions) that operate on
the data into a single unit or class, and restricting access to some of the object's
components.
2. Abstraction: Hiding the complex implementation details and showing only the
necessary features of an object.
3. Inheritance: A mechanism where a new class (subclass) inherits properties and
behaviors (methods) from an existing class (superclass).
4. Polymorphism: The ability of different classes to be treated as instances of the same
class through a common interface, typically through method overriding or overloading.
v) Size of Primitive Data Types in Java
● byte: 8 bits (1 byte)
● short: 16 bits (2 bytes)
● int: 32 bits (4 bytes)
● long: 64 bits (8 bytes)
● float: 32 bits (4 bytes)
● double: 64 bits (8 bytes)
● char: 16 bits (2 bytes)
● boolean: Not precisely defined but usually 1 bit.
2. Solve the Following Logical Expressions:
i) Output for the Following:
● (a) 5 * ++x;
○ ++x increments x before the multiplication. If x was 5, then ++x would make it 6.
Therefore, 5 * 6 = 30.
● (b) 5 * x++;
○ x++ increments x after the multiplication. If x was 5, then 5 * 5 = 25. x would
then be incremented to 6.
ii) Output for the Expression: a += a++ + ++a + --a + a--; when a = 7
● Breaking it down:
○ a++ (post-increment): 7, a becomes 8.
○ ++a (pre-increment): a is now 9.
○ --a (pre-decrement): a is now 8.
○ a-- (post-decrement): a is 8, then becomes 7.
● So, a += 7 + 9 + 8 + 8 which results in a += 32. Initially, a was 7, so it becomes
39.
iii) Output of the Following:
java
Copy code
char g = 'k';
int n = g + 3;
int m = g + n;
[Link](m);
● g is 'k' which is ASCII value 107. So n = 107 + 3 = 110.
● m = 107 + 110 = 217.
Output: 217
iv) Rewrite the Code Using Ternary Operator:
java
Copy code
grade = (marks >= 90) ? "A" :
(marks >= 80) ? "B" : "C";
e) Type of Error in Each Case:
● i) To find the square root of a negative number: This will result in a runtime error,
specifically a mathematical error if you are using standard functions like [Link] in
Java, which cannot handle negative inputs without complex number handling.
● ii) Addition operator used when the operation should be multiplication: This is a
logical error.
3. Write Down the Following Expressions in Java:
java
Copy code
// a)
a2 + b2 + c3
// b)
2 * (lb + bh + lh)
// c)
[Link](mn) + [Link](mn, 3)
// d)
[Link]([Link](x, 3) - [Link](y, 2) - 2 * x * y)
// e)
ut + 0.5 * a * [Link](t, 2)
4. Output of the Following Functions:
java
Copy code
a) [Link](-15.5, -19.5); // Output: -19.5
b) [Link](144, 1/2) + [Link](144); // Output: 12 + 12 = 24 (Note:
1/2 is integer division, so it equals 0)
c) [Link](3.4) + [Link](2, 3); // Output: 4 + 8 = 12
d) [Link]([Link](-8.7)); // Output: 8.0
e) [Link]([Link](14.55), 15.51); // Output: 15.51
5. Rewrite Using if-else Statement:
java
Copy code
String st;
if (a > 0) {
if (a % 2 == 0) {
st = "Positive even";
} else {
st = "Positive odd";
}
} else {
st = "Negative number";
}
6. Program to Calculate Average Marks and Display Stream:
java
Copy code
import [Link];
public class StreamSelector {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter Physics marks: ");
int physics = [Link]();
[Link]("Enter Chemistry marks: ");
int chemistry = [Link]();
[Link]("Enter Biology marks: ");
int biology = [Link]();
double average = (physics + chemistry + biology) / 3.0;
if (average >= 80) {
[Link]("Stream: Computer Science");
} else if (average >= 60) {
[Link]("Stream: Bio-Science");
} else if (average >= 40) {
[Link]("Stream: Commerce");
} else {
[Link]("Stream: None");
}
[Link]();
}
}
7. Volume Calculation Program Using Switch Case:
java
Copy code
import [Link];
public class VolumeCalculator {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Select the solid (1 - Cuboid, 2 -
Cylinder, 3 - Cone): ");
int choice = [Link]();
switch (choice) {
case 1: // Cuboid
[Link]("Enter length: ");
double l = [Link]();
[Link]("Enter breadth: ");
double b = [Link]();
[Link]("Enter height: ");
double h = [Link]();
double volumeCuboid = l * b * h;
[Link]("Volume of Cuboid: " +
volumeCuboid);
break;
case 2: // Cylinder
[Link]("Enter radius: ");
double r = [Link]();
[Link]("Enter height: ");
h = [Link]();
double volumeCylinder = [Link] * [Link](r, 2) * h;
[Link]("Volume of Cylinder: " +
volumeCylinder);
break;
case 3: // Cone
[Link]("Enter radius: ");
r = [Link]();
[Link]("Enter height: ");
h = [Link]();
double volumeCone = (1.0/3.0) * [Link] * [Link](r,
2) * h;
[Link]("Volume of Cone: " + volumeCone);
break;
default:
[Link]("Invalid choice");
}
[Link]();
}
}
8. Pythagorean Triplet Program:
java
Copy code
import [Link];
public class PythagoreanTriplet {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter first number: ");
int a = [Link]();
[Link]("Enter second number: ");
int b = [Link]();
[Link]("Enter third number: ");
int c = [Link]();
int x = [Link](a, [Link](b, c));
int y = (x == a) ? b : (x == b) ? a : b;
int z = (x == a) ? c : (x == b) ? c : a;
if (x * x == y * y + z * z) {
[Link]("The numbers form a Pythagorean
Triplet");
} else {
[Link]("The numbers do not form a Pythagorean
Triplet");
}
[Link]();
}
}
9. Radius of a Sphere:
java
Copy code
import [Link];
public class SphereRadius {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter volume of the sphere: ");
double volume = [Link]();
double radius = [Link]((3 * volume) / (4 * [Link]),
1.0/3.0);
[Link]("Radius of the sphere: " + radius);
[Link]();
}
}
10. Employee Class:
java
Copy code
class Employee {
double basic;
Employee(double basic) {
[Link] = basic;
}
double calculateGrossPay() {
double DA = 0.25 * basic;
double HRA = 0.15 * basic;
double PF = 0.0833 * basic;
double NetPay = basic + DA + HRA;
double GrossPay = NetPay - PF;
return GrossPay;
}
public static void main(String[] args) {
Employee emp = new Employee(50000); // Example basic pay
[Link]("Gross Pay: " + [Link]());
}
}