20. What is the purpose of HAVING clause ? How is it different from WHERE clause in SQL ? Give example to support you answer.
Ans: The HAVING clause in SQL is used to filter the results of a SQL query that includes a GROUP BY clause.
The main difference between the HAVING and WHERE clauses is when they are applied:
• The WHERE clause is used to filter rows before they are grouped and is applied to individual rows in the table.
• The HAVING clause is used to filter the results of the grouping after the GROUP BY operation has been performed, and it is applied to groups of rows based on the results of aggregate functions.
SELECT CustomerID, SUM(Quantity) as TotalQuantity
FROM Orders
WHERE Product = 'Widget'
GROUP BY CustomerID
HAVING TotalQuantity > 100;
SELECT CustomerID, SUM(Quantity) as TotalQuantity
FROM Orders
WHERE Product = 'Widget'
GROUP BY CustomerID
HAVING SUM(Quantity) > 100;
21. Write advantages of online tutorials.
Ans: Online tutorials offer several advantages, including:
Convenience and Flexibility: Learners can access online tutorials from anywhere, at any time, making it convenient for them to fit learning into their schedules. This flexibility allows for self-paced learning.
Variety of Topics: Online tutorials cover a wide range of subjects and skills, making it easy to find tutorials on specific topics of interest or need.
Cost-Effective Learning: Many online tutorials are available for free or at a lower cost than traditional classroom courses.
Interactive Learning: Online tutorials often incorporate multimedia, quizzes, and interactive exercises, making the learning experience engaging and effective.
22. Why is Java considered as an Object Oriented Programming language ? Explain data members and member methods with the help of an example.
Ans: Java is considered an Object-Oriented Programming (OOP) language because it follows the principles and concepts of Object-Oriented Programming. These principles include encapsulation, inheritance, polymorphism, and abstraction.
In an OOPs language, a program is a collection of objects that interact with other objects to solve a problem. Each object is an instance of a class.
Without the creation of objects and classes, it is impossible to write any code in Java.
Data members and member methods with an example in Java:
class Bicycle
{
private int gear=5;
public void braking ()
{
System.out.println(“Working of Braking”);
}
}
In this example, the Car class represents an object in an Object-Oriented manner. It has data members (attributes) like brand, model, year, and price, and member methods like startEngine, accelerate, and calculateResaleValue. The use of encapsulation is evident through private data members and public getters and setters to access and modify these attributes. The class illustrates the principles of OOP, including encapsulation and member methods.
23. (i) ‘Java is a platform independent language.’ Justify the statement.
Ans: Java's platform independence means that Java code can run on different computer systems without modification. It achieves this through an intermediate step: when you write Java code, it gets compiled into a special form called bytecode, which is not tied to any specific computer. Then, a program called the Java Virtual Machine (JVM) on each computer translates this bytecode into instructions that the computer can understand. Since the JVM is available on various platforms, Java programs work on Windows, macOS, Linux, and more.
(ii) Give two relational operators and two logical operators in Java.
Ans: Relational Operators:
(i) Equal to (= =): Used to check if two values are equal. For example: if (x == y) checks if x and y have the same value.
(ii) Not equal to (!=): Used to check if two values are not equal. For example: if (a != b) checks if a and b have different values.
Logical Operators:
(i) Logical AND (&&): Used to combine two conditions. It returns true if both conditions are true. For example: if (condition1 && condition2) checks if both condition1 and condition2 are true.
(ii) Logical OR (||): Used to combine two conditions. It returns true if at least one of the conditions is true. For example: if (conditionA || conditionB) checks if either conditionA or conditionB is true.
24. Write the output of following code:
(i) class production
{
int inp = 50;
void change(int inp)
{
System.out.println("Change"+po.inp);
inp=inp+100;
}
public static void main(String args[])
{
production po=new production();
System.out.println("before change"+po.inp);
po.change(500);
System.out.println("after change"+po.inp);
}
}
Ans:
before change 50
Change 50
after change 50
(ii) What are the two ways to write comments in Java?
Ans: In Java, there are two ways to write comments:
(i) Single-Line Comments: Single-line comments are used to comment a single line or a portion of a single line. You can create a single-line comment by using //. Anything following // on the same line is treated as a comment and is ignored by the compiler.
(ii) Multi-Line Comments: Multi-line comments are used to comment multiple lines or a block of code. You can create a multi-line comment by enclosing the comments within /* and */. Anything between these delimiters is considered a comment and is ignored by the compiler.