Model Question Solution Java

Model Question Solution
  1. What are the uses of final modifier ? Explain each use of the modifier with suitable example. (1+9)
  1. variable
  2. method
  3. class
  1. Variable
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
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
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
index.html
input html form
Output
Output
  • Flow Layout
  • Border Layout
  • Grid Layout
  • Gridbag layout
  • Card Layout
  • Group Layout
Output
Output
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();
}
}
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();
}
}
Ouptut
Difference Between Swing and JavaFX
  1. Extend javafx.application.Application and override start()
import javafx.application.Application;public class HelloWorld extends Application {
public void start(Stage primaryStage)
{
//Other steps ...
}
}
Button btn=new Button("Say, Hello World");
StackPane root =new StackPane();
root.getChildren().add(btn);
Scene scene =new Scene (root,400,300);
primaryStage.setScene(scene);
primaryStage.setTitle("First JavaFX Application");
primaryStage.show();
button.setOnAction(new Handler());
}
class Handler implements EventHandler<ActionEvent>
{
@Override
public void handle(ActionEvent ae)
{
System.out.println("Hello World");
}
}
public static void main(String[] args)
{
launch(args)
}
  • By implementing the Servlet interface.
  • By inheriting the GenericServlet class.
  • By inheriting the 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
}
}
Difference of RMI and CORBA
float calculate_tax(in float amount);
Consistent output using synchronized method

--

--

--

Learn Code with Fun.

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

IPC: Inter-process communication in C programming

What are the Best Practices for AWS Monitoring for a Business Enterprise?

AWS Services

Argument list too long

How I Passed My AWS SysOps Associate Exam

A Simple Guide to Real-Time Speech Recognition in Python

Summer 2021 — Task 1

The 19 Best CMMS Resources on the Web

Use redshift-data api with AWS Glue Python Shell job

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Code With Arjun

Code With Arjun

Learn Code with Fun.

More from Medium

Data types in JVM

Java Date,Time With Data type and Epoch

Multithreading in Java

Threads in Java