Q.20 Write a Java code using String methods to perform the following tasks:
String str = "java programming";
i. Convert all characters to lowercase.
Ans: String lowerStr = str.toLowerCase();
System.out.println("Lowercase: " + lowerStr);
ii. Find the total number of characters in the string.
Ans: int length = str.length();
System.out.println("Length: " + length);
iii. Append the word "rocks" at the end of the string.
Ans: String appendedStr = str + " rocks";
System.out.println("Appended: " + appendedStr);
iv. Replace the word "java" with "Python".
Ans: String replacedStr = str.replace("java", "Python");
System.out.println("Replaced: " + replacedStr);
Q.21 List and briefly explain the four main phases in the development of a web-based application project.
Ans: Four main phases in the development of a web-based application project:
(i) Requirements Definition Phase: Identify the problem, determine project feasibility, define the scope, list required features, and set project goals.
(ii) Design Phase: Plan the structure and layout of the application, including user interface, database design, and system architecture.
(iii) Implementation Phase: Develop the application by writing code and integrating different components according to the design.
(iv) Testing Phase: Test the application to find and fix errors or bugs and ensure that it works properly before deployment.
Q.22 What are multithreaded programs in Java? What are the two ways by which these threads can be created?
Ans: A multithreaded program is a program that can perform multiple tasks simultaneously to make better use of the computer's resources. It consists of two or more parts called threads, where each thread executes a task independently at the same time.
Threads in Java can be created in two ways:
(i) By extending the Thread class
(ii) By implementing the Runnable interface
Q.23 (a) Define arrays.
Ans: An array is a collection of elements of the same data type stored in contiguous memory locations and accessed using an index.
(b) Write a program to:
i) Create a string array Names containing five names.
Ans: String[] Names = {"Amit", "Rina", "Anil", "Asha", "Ravi"};
ii) Display each name using a while loop.
Ans: int i = 0;
while(i < Names.length)
{
System.out.println(Names[i]);
i++;
}
(c) What happens if a program tries to access an array element using an index that is outside the valid range?
Ans: If a program tries to access an array element using an index that is outside the valid range, an ArrayIndexOutOfBoundsException occurs and the program terminates with an error message.
Q.24 Consider the following tables:
Table: MOVIE
| MID | MName | Genre | Budget |
|---|
| M01 | Avatar | Sci-Fi | 500000 |
| M02 | Dangal | Sports | 150000 |
| M03 | Inception | Thriller | 300000 |
Table: ACTOR
| AID | MID | ActorName |
|---|
| A101 | M01 | Sam Worthington |
| A102 | M02 | Aamir Khan |
| A103 | M03 | Leonardo DiCaprio |
Write SQL queries for the following:
(a) Find the total number of actors in each movie.
Ans: SELECT MID, COUNT(*) AS TotalActors
FROM ACTOR
GROUP BY MID;
(b) Add a new record to MOVIE: (M04, '3 Idiots', 'Comedy', 200000).
Ans: INSERT INTO MOVIE VALUES ('M04', '3 Idiots', 'Comedy', 200000);
(c) Display MName, ActorName, and Genre of all actors.
Ans: SELECT MName, ActorName, Genre
FROM MOVIE, ACTOR
WHERE MOVIE.MID = ACTOR.MID;
(d) Show details of movies whose budget is more than 200000.