Model Question Solution Java
Bsc. CSIT (7th Sem) | Course Code : CSC 409

- What are the uses of final modifier ? Explain each use of the modifier with suitable example. (1+9)
Answer :
The final keyword is a non-access modifier in java is used to restrict the user. The keyword can be used in 3 different context :
- variable
- method
- class
which are explained in details as below :
- Variable
If you make any variable as final, you cannot change the value of final variable.
For example :
class Bike{
final int speedLimit=90; //final variable
void ride(){
speedLimit=900;
}
public static void main(String[] args){Bike obj=new Bike();
obj.ride();
}
}Output : Compile Time error
The program will give compile time error because speedLimit is final variable, which means we can’t change the value.
2. Method
If you make any method as final you can’t override it.
For example :
class Bike{
final void ride() //final method
{
System.out.println("Riding");
}class Gixxer extends Bike{
void ride (){
System.out.println("I am riding bike with speed of 80km/h");
}public static void main(String[] args)
{
Gixxer gixxer=new Gixxer();
gixxer.ride();
}
}Output : Compile Time error
The above program again gives compile time error since we are trying to override ride() method in Gixxer class.
3. Final Class
If you make any class as final, you cannot extend it.
final class Bike{} //final classclass Gixxer extends Bike{
void ride(){
System.out.println("Riding safely with 100km/h");
}public static void main(String[] args){
Gixxer gixxer=new Gixxer();
gixxer.ride();
}
}Output : Compile Time error
The program will give compile time error since we are trying to extend Bike class.
2. Write a java program to create login with user id, password, ok button, and cancel button. Handle key events such that pressing ‘l’ performs login and pressing ‘c’ clears text boxes and puts focus on user id text box. Assume user table having fields Uid and Password in the database named account. (10)
LoginFrame.java
LoginService.java
3.Discuss various scopes of JSP objects briefly. Create HTML File with principal, time and reate. Then crate a JSP file that reads values from the HTML form, calculates simple interest and displays it.
Answer:
The availability of a JSP object for use from a particular place of the application is defined as the scope of that JSP object.Object scope in JSP is segregated into four parts and they are page, request, session and application.
Page Scope :
Page scope means, the JSP object accessed only from within the same page where it was created. JSP implicit objects out, exception, response, pageContext, config and page have page scope.
//Example of JSP Page Scope
<jsp:useBean id=”employee” class=”EmployeeBean” scope=”page”/>
Request Scope :
A JSP object created using the request scope can be accessed from any pages that serves that request. More than one page can serve a single request. Implicit object request has the request scope.
//Exmple of JSP Request Scope
<jsp:useBean id=”employee” class=”EmployeeBean” scope=”request”/>
Session Scope :
Session scope means, the JSP object is accessible from pages that belong to the same session from where it was created. Implicit object session has the session scope.
//Example of JSP Session scope
<jsp:useBean id=”Employee” class=”EmployeeBean” scope=”session”/>
Application Scope
A JSP object created using the application scope can be accessed from any pages across the application. The implicit object application has the application scope.
//Ecmaple of JSP application scope.
<jsp:useBean id=”Employee” class=”EmployeeBean” scope=”application”/>
Second Part Answer :
index.html
index.jsp
Output :


Group ‘B’
4. Write a java program that writes objects of Employee class in the file named emp.doc. Create Employee class as of you interest.(5)
Output :

Note : You can skip FileInputStream Part to read object since it’s only asked to write object in question.
5. What are layout managers? Explain Gridbag layout with suitable example.
The LayoutManagers are used to arrange components in a particular manner. The Java LayoutManagers facilitates us to control the positioning and size of the components in GUI forms. The layout manager is set by the setLayout() method.
Some of the most commonly used layout mangers of java are :
- Flow Layout
- Border Layout
- Grid Layout
- Gridbag layout
- Card Layout
- Group Layout
GridBag Layout :
The GridBag layout is a flexible layout manger that aligns components vertically and horizontally, without requiring that the components be of the same size.
For Example :
Output :

6. What is the use of action command in event handling ? Explain with example.
Answer :
Action command is used to handle event caused by the Buttons.
In the above example if clicked on the Demo Button, with the help of getActionCommand() method we will be able to see the click event done on the output .

7. What causes SQL exception ? How it can be handled ? Explain with example.
Answer:
SQL exception can be occured either if driver is missing or information about database is wrong or SQL query is wrong.
SQL exception can be handled by utilizing the information available from the Exception object, which can be caught with an exception.
For Example :
The Output of the above program will be “Invalid SQL Statement” since we are passing wrong query , Hence SQLException caught the exception and printed the message.
8. Write a java program using TCP such that client sends number to server and displays it’s factorial. The server computes factorial of the number received from client. (5)
MyServer.java
import java.io.*;
import java.net.*;
public class MyServer {
public static void main(String[] args) throws IOException {
ServerSocket ss=new ServerSocket(1234);
System.out.println("Waiting for client request");
Socket s=ss.accept();
String str;
BufferedReader br=new BufferedReader(new InputStreamReader(s.getInputStream()));
str=br.readLine();
System.out.println("value received");
int number=Integer.parseInt(str);
int i,fact = 1;
for(i=1;i<=number;i++){
fact=fact*i;
}
PrintStream ps=new PrintStream(s.getOutputStream());
ps.println(fact);
br.close();
ps.close();
s.close();
ss.close();
}
}
MyClient.java
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.net.Socket;
public class MyClient {
public static void main(String[] args) throws IOException {
Socket s = new Socket("localhost", 1234);
String str;
System.out.println("Enter any number :");
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
str = br.readLine();
PrintStream ps = new PrintStream(s.getOutputStream());
ps.println(str);
BufferedReader fs = new BufferedReader(new InputStreamReader(s.getInputStream()));
String result = fs.readLine();
System.out.println("Factorial is :" + result);
br.close();
fs.close();
ps.close();
s.close();
}
}
Output :

9.How JavaFX differs from Swing ? explain steps of creating GUI using JavaFX.(2+3)
Answer: Difference between Swing and JavaFX:

Steps for writing JavaFX Program :
- Extend javafx.application.Application and override start()
import javafx.application.Application;public class HelloWorld extends Application {
public void start(Stage primaryStage)
{//Other steps ...
}}
2. Create a Button
Button btn=new Button("Say, Hello World");
3. Create a layout and add button to it
StackPane root =new StackPane();
root.getChildren().add(btn);
4. Create a Scene
Scene scene =new Scene (root,400,300);
5. Preapare the Stage
primaryStage.setScene(scene);
primaryStage.setTitle("First JavaFX Application");
primaryStage.show();
6. Create an event for the button
button.setOnAction(new Handler());
}
class Handler implements EventHandler<ActionEvent>
{
@Override
public void handle(ActionEvent ae)
{
System.out.println("Hello World");
}
}
7. Create the main method
public static void main(String[] args)
{
launch(args)
}
For Full code refer this link JavaFXDemo
10. What are different ways of writing servlet programs ? Write a sample Servlet program using any one way. (1+4)
Answer :
Different ways of writing servlet programs are :
- By implementing the Servlet interface.
- By inheriting the GenericServlet class.
- By inheriting the HttpServlet class.
Servlet Program by inheriting HttpServlet class :
import javax.servlet.http.*;
import javax.servlet.*;
import java.io.*;public class DemoServlet extends HttpServlet
{
public void doGet(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException
{
res.setContentType("text/html"); //setting the content type
PrintWriter pw=res.getWriter(); //get the stream to write the data
//writing the html in stream
pw.println("<html><body>");
pw.println("Welcome to servlet");
pw.println("</body></html>);
pw.close(); //closing the stream
}
}
Output : Welcome to servlet
If you want to get the detailed video on how can create simple jsp servlet program watch this video :
11. How CORBA differs from RMI ? Discuss the concepts of IDL briefly. (2+3)

Second Part :
An Interface Description Language (IDL) is a specification language used to describe a software component’s interface. IDLs describe an interface in a language-independent way, enabling communication between software components that do not share a language- for example, between components written in C++ and components written in java.
We can’t write actual program in IDL because it lacks the logic and flow control structure, instead IDL concentrates on type definations, both for primitive and class types, so you can use it instead to define interface, not implementation. For example, and function used to calculate tax on an order might be defined like this :
float calculate_tax(in float amount);
12. When thread synchronization is necessary ? Explain with suitable example.
Answer :
Java Thread synchronization is necessary where we want to allow only one thread to access the shared resource. So Synchronization in Java gives the capability to control the access of multiple threads to any shared resource.
For example :
Output :

Connect me :
Facebook : https://www.facebook.com/profile.php?id=100010140661075
Github : https://github.com/arjungautam1
LinkedIn :https://np.linkedin.com/in/arjungautam1
Youtube : https://www.youtube.com/c/ArjunCodes