Download as pdf or txt
Download as pdf or txt
You are on page 1of 21

Spring End Semester Examination 2023

4th Semester B.Tech.


Web Technology (IT 2004)
Evaluation Scheme

Q1. Answer the following questions.


(a) How to specify a hyperlink in HTML and display the linked page in the new
window or tab using target attribute?
Ans: To create a hyperlink in HTML code and display the linked page in a new
window or tab we use anchor <a> tag and “target” attribute along with “href”
attribute.
<a href="https://www.abc.com" target="_blank"> Hyperlink Demo</a>
The target attribute is use to define "_blank", which indicates that the linked page
should be opened in a new browser window or tab.

(b) Which loop structure can be used for an array without referring to the elements
by index?
Ans: In Java, the "foreach" loop is also known as the enhanced for loop. It is used
to is used to iterate through elements of arrays or collections without explicitly
using an index or iterator. For example:
int[] arr = {1, 2, 3, 4, 5};
for(int i : arr)
System.out.println(i);

(c) Identify the correct restriction on static methods.


I. They must access only static data.
II. They can call other static methods and non static method directly.
III. They cannot refer to this or super.
A. I and II
B. I, II and III
C. Only III
D. I and III
Ans: A

(d) What will be the output of the following program?


class A {
public A() {
System.out.println("Base");
}
}
class B extends A {
public Derived() {
System.out.println("Derived");
}
}
class C extends B {
public DeriDerived() {
System.out.println("DeriDerived");
}
}
public class Demo {
public static void main(String[] args) {
B b = new C( );
}
}
Ans: Base

(e) Select the correct option for derived class <access – modifier>
public class A{
protected void display() {
System.out.println("Display-base");
}
}
public class B extends A {
< access - modifier > void display() {
System.out.println("Display-derived");
}
}
1. Only protected can be used.
2. public and protected both can be used.
3. public, protected, and private can be used.
4. Only public can be used.
Ans: 2
Because derived class overriding method access modifier must be same or weaker
access privilege.

(f) Which of the following is false statement about interface in java?


1. An interface can extend multiple interfaces.
2. All constant values defined in an interface are implicitly public, static, and final.
3. The interface body can contain abstract methods, default methods, and static
methods.
4. Dynamic method object reference cannot be achieved using interface.
Ans: 4

(g) What is the use of printStackTrace( ) method and from which class it belongs?
Ans: The printStackTrace() method prints information about Exception, which
occurred during program execution. It prints three information an exception such
as name of the exception class, short description about exception and stack trace
(line number where exception occurred). This method belongs to Throwable class
which is the superclass of Exception and Error classes.
(h) What will be the output of the following program?
public class Test
{
public static void main(String[] args)
{
StringBuffer sb = new StringBuffer("KIIT");
System.out.println(sb.capacity());
}
}
Ans: 20

(i) What are the three standard Java IO streams and what is each used for?
Ans: There are three standard IO streams as follows:
System.in, System.out, and System.err
System.in is a standard input stream use to receive input from keyboard. “in” is a
reference variable of InputStream class and a static member of System class.
System.out is a standard output stream use to display output in the
console/terminal. “out” is a reference variable of PrintStream class and a static
member of System class.
System.err is a standard output stream use to output error texts in the
console/terminal. “err” is a reference variable of PrintStream class and a static
member of System class.

(j) What is the use of PARAM tag/element in Applet? Explain PARAM tag with its
attributes.
Ans: In Applet, the <param> tag is used to pass input parameters from an HTML
page to the Java applet embedded within it. The <param> tag allows us to pass
values that can be accessed by the applet during its initialization or runtime.
For example:
<applet code="MyApplet.class" width="200" height="200">
<param name= "par1" value= "value1">
<param name="par2" value="value2">
</applet>

Q2.a)
<ul>
<li>DAL</li>
<ol>
<dl>
<li>urad dal</li>
<dd> contains proteins</dd>
<li>chana dal</li>
<dd> contains proteins</dd>
<li> masoor dal</li>
<dd> contains proteins</dd>
</dl>
</ol>
<li>oil
<ol>
<li>mustard oil</li>
<li>almond oil</li>
<li> castor oil</li>
</ol>
</li>
<li>Milk</li>
</ul>

I have written the code for 2 items of the list, so the same will be applied for 3 more items.

Q2.b)

import java.util.*;

public class Main {


static int array[];
static int n;
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the length of the array: ");
n = scanner.nextInt();
System.out.print("Enter the lower bound of the range: ");
int lowerBound = scanner.nextInt();
System.out.print("Enter the upper bound of the range: ");
int upperBound = scanner.nextInt();

//initializing the array


array = new int[n];
for (int i = 0; i < n; i++) {
int number = (int)(Math.random()*upperBound) + lowerBound;
array[i] = number;
}

//arranging the array


int lastPrimeIndex = -1;
for (int i = 0; i < n - 1; i++) {
for (int j = i + 1; j < n; j++) {
if (!isPrime(array[i]) && isPrime(array[j])) {
int temp = array[i];
array[i] = array[j];
array[j] = temp;
lastPrimeIndex = i;
break;
}
}
}

//for sorting the prime section


for (int i = 0; i <= lastPrimeIndex; i++) {
int minIndex = i;
for (int j = i + 1; j <= lastPrimeIndex; j++) {
if (array[j] < array[minIndex]) {
minIndex = j;
}
}
int temp = array[i];
array[i] = array[minIndex];
array[minIndex] = temp;
}

//for sorting the non prime section


for (int i = lastPrimeIndex + 1; i < n; i++) {
int minIndex = i;
for (int j = i + 1; j < n; j++) {
if (array[j] < array[minIndex]) {
minIndex = j;
}
}
int temp = array[i];
array[i] = array[minIndex];
array[minIndex] = temp;
}
System.out.println(Arrays.toString(array));
System.out.println("Array after rearrangement and sorting:");
System.out.println(Arrays.toString(array));
}

private static boolean isPrime(int number) {


if (number < 2) {
return false;
}
for (int i = 2; i * i <= number; i++) {
if (number % i == 0) {
return false;
}
}
return true;
}
}

Question 3A Solution.
Difference between HTTP GET and HTTP POST
Evaluation Scheme: 1 mark can be awarded for writing any of two differences from the
following

HTTP GET HTTP POST


In GET method we can not send large In POST method large amount of data can be
amount of data rather limited data is sent sent because the request parameter is
because the request parameter is appended appended into the body.
into the URL.
Data passed through GET method can be Data passed through POST method can not
easily stolen by attackers. be easily stolen by attackers.
GET request is comparatively less secure POST request is comparatively more secure
because the data is exposed in the URL bar. because the data is not exposed in the URL
bar.
Request made through GET method are Request made through POST method is not
stored in Browser history. stored in Browser history.
Request made through GET method are Request made through POST method are not
stored in cache memory of Browser. stored in cache memory of Browser.
In GET method only ASCII characters are In POST method all types of data is allowed.
allowed
Evaluation Scheme: 3 marks for designing registration form with given input fields
<!DOCTYPE html>
<html lang="en">
<head>
<title>HTML Registration Form</title>
</head>
<body>
<form>
<fieldset>
<legend>Registration form</legend>
Name: <input type="text"><br>
Address:<br><textarea rows="10" cols="20">Enter your address</textarea><br>
Email:<input type="email"><br>
Phone:<input type="tel"><br>
Age:<input type="number"><br>
Gender:<br>
<input type="radio">Male<br>
<input type="radio">Female<br>
<input type="radio"> Other<br>
Marital Status:<br>
<input type="checkbox">Married<br>
<input type="checkbox">UnMarried<br>
State:<br>
<select>
<option>Odisha</option>
<option>Maharashtra</option>
<option>West Bengal</option>
<option>Madhya Pradesh</option>
<option>Bihar</option>
<option>Uttar pradesh</option>
</select><br>
<input type="submit" value="submit">
<input type="reset" value="reset">
</fieldset>
</body>
</html>

Question 3 B Solution.

Evaluation Scheme: Correct logic to reverse k elements in the array with all input and display
functions 4 Marks
Partially correct 1 to 2 marks may be awarded

import java.util.Scanner;
class Test{
int size=5;
int i,j;
int arr[]=new int[size];
static Scanner sc=new Scanner(System.in);
void input(){
System.out.println("Enter elements into array");
for (i=0;i<size;i++){
arr[i]=sc.nextInt();
}
}
void reverse_K_Elements(int arr[], int k){
for (int i = k; i < arr.length; i++) {
for (int j = i + 1; j < arr.length; j++)
{
int tmp = 0;
if (arr[i] > arr[j])
{
tmp = arr[i];
arr[i] = arr[j];
arr[j] = tmp;
}
}
}
}

void display(){
for (i=0;i<size;i++){
System.out.println(arr[i]);
}
}
public static void main(String args[]){
int k;
Test t=new Test();
t.input();
System.out.println("Enter K value ");
k=sc.nextInt();
t.reverse_K_Elements(t.arr,k);
t.display();

}
}
Q4) how many ways css can be specified for html elements with examples? Define
CSS selectors and its type with examples.
Ans: CSS can be specified for html elements in three ways:
 Inline- by using <style> attribute inside HTML elements
 Internal- by using <style> attribute in the <Head> section
 External- by using s <link> element to link to an external CSS file
Student has to explain all the three ways with one one example.

CSS selectors are used to select the content you want to style. Selectors are the part of CSS
rule set. CSS selectors select HTML elements according to its id, class, type, attribute etc.

There are several different types of selectors in CSS.

1. CSS Element Selector

2. CSS Id Selector

3. CSS Class Selector

4. CSS Universal Selector

5. CSS Group Selector

1) CSS Element Selector

The element selector selects the HTML element by name.

<!DOCTYPE html>
<html>
<head>
<style>
p{
text-align: center;
color: blue;
}
</style>
</head>
<body>
<p>This style will be applied on every paragraph.</p>
<p id="para1">Me too!</p>
<p>And me!</p>
</body>
</html>

2) CSS Id Selector
The id selector selects the id attribute of an HTML element to select a specific element. An id
is always unique within the page so it is chosen to select a single, unique element.

It is written with the hash character (#), followed by the id of the element.

<!DOCTYPE html>
<html>
<head>
<style>
#para1 {
text-align: center;
color: blue;
}
</style>
</head>
<body>
<p id="para1">Hello Javatpoint.com</p>
<p>This paragraph will not be affected.</p>
</body>
</html>

3) CSS Class Selector

The class selector selects HTML elements with a specific class attribute. It is used with
a period character . (full stop symbol) followed by the class name.

<!DOCTYPE html>
<html>
<head>
<style>
.center {
text-align: center;
color: blue;
}
</style>
</head>
<body>
<h1 class="center">This heading is blue and center-aligned.</h1>
<p class="center">This paragraph is blue and center-aligned.</p>
</body>
</html>

4) CSS Universal Selector

The universal selector is used as a wildcard character. It selects all the elements on
the pages.

!DOCTYPE html>
<html>
<head>
<style>
*{
color: green;
font-size: 20px;
}
</style>
</head>
<body>
<h2>This is heading</h2>
<p>This style will be applied on every paragraph.</p>
<p id="para1">Me too!</p>
<p>And me!</p>
</body>
</html>

5) CSS Group Selector

The grouping selector is used to select all the elements with the same style
definitions.

Grouping selector is used to minimize the code. Commas are used to separate each
selector in grouping.

h1,h2,p {
text-align: center;
color: blue;
}
<!DOCTYPE html>
<html>
<head>
<style>
h1, h2, p {
text-align: center;
color: blue;
}
</style>
</head>
<body>
<h1>Hello Javatpoint.com</h1>
<h2>Hello Javatpoint.com (In smaller font)</h2>
<p>This is a paragraph.</p>
</body>
</html>

Evaluation Note: Due to a printing error in 2nd part of this question (CSS
sector printed instead of CSS selector), a grace of 2 marks may be awarded for
genuine cases.

Q4b) Explain the importance of Dynamic Method Dispatch Concept.


Write a program to implement Dynamic Method Dispatch Concept.
Ans: It allows a class to define methods that will be shared by all its derived
classes, while also allowing these sub-classes to define their specific
implementation of a few or all of those methods. It allows subclasses to
incorporate their own methods and define their implementation.
example illustrating dynamic method dispatch:

class Apple
{
void display()
{
System.out.println("Inside Apple's display method");
}
}
class Banana extends Apple
{
void display() // overriding display()
{
System.out.println("Inside Banana's display method");
}
}

class Cherry extends Apple


{
void display() // overriding display()
{
System.out.println("Inside Cherry's display method");
}
}

class Fruits_Dispatch
{
public static void main(String args[])
{
Apple a = new Apple(); // object of Apple
Banana b = new Banana(); // object of Banana
Cherry c = new Cherry(); // object of Cherry

Apple ref; // taking a reference of Apple

ref = a; // r refers to a object in Apple


ref.display(); // calling Apple's version of display()

ref = b; // r refers to a object in Banana


ref.display(); // calling Banana's version of display()

ref = c; // r refers to a object in Cherry


ref.display(); // calling Cherry's version of display()
}
}
Q 5 (a) Package in Java is a mechanism to encapsulate a group of classes, sub packages and
interfaces. Packages are used for:
 Preventing naming conflicts. For example there can be two classes with name
Employee in two packages, college.staff.cse.Employee and college.staff.ee.Employee
 Making searching/locating and usage of classes, interfaces, enumerations and
annotations easier
 Providing controlled access: protected and default have package level access control.
A protected member is accessible by classes in the same package and its subclasses. A
default member (without any access specifier) is accessible by classes in the same package
only.
 Packages can be considered as data encapsulation (or data-hiding).

/** Find_Factorial4.java */
import factorial.Factorial;//import Factorial class
import java.util.Scanner;//import Scanner class
public class Find_Factorial4{
public static void main(String args[]){
Scanner scan=new Scanner(System.in);
//create a scanner object
System.out.println("Enter a number for find
factorial");
int num=scan.nextInt();//get input from user
factorial(num);//call the method
}

/** Factorial.java */
package factorial;
public class Factorial {
static void factorial(int num)//user defined method
for calculate factorial
{
int i,f=1;
for(i=1; i<=num; i++){
f=f*i;//calculate te factorial using for loop
}
System.out.print("Factorial of the "+num+"is "+f);
//display the factorial
}
}
}

(b) abstract class AbstractClassTest {

AbstractClassTest(int a) { // Parameterized Constructor

System.out.println("Parameterized Constructor of an abstract class a="+ x);

}}public class Test extends AbstractDemo {


Test() {

super(20);

System.out.println("Test Class Constructor");

}
public static void main(String[] args) {

Test obj = new Test();

}}

an abstract class can have a constructor in Java. You can either explicitly provide a
constructor to the abstract class or if you don't, the compiler will add a default constructor of
no argument in the abstract class. This is true for all classes and it also applies to an abstract
class.
public class Main
{
public static void main(String[] args) {
A a = new A(70, 50, 100);
System.out.println(a.getPercentage());
B b = new B(90, 75, 64, 86);
System.out.println(b.getPercentage());
}
}
abstract class Marks {
public abstract float getPercentage();
}
class A extends Marks{
int marks1, marks2, marks3;
A(int m1, int m2, int m3){
marks1=m1;
marks2=m2;
marks3=m3;
}
public float getPercentage(){
float total=((marks1+marks2+marks3)/(float)300)*100;
return total;
}
}
class B extends Marks{
int marks1, marks2, marks3, marks4;
B(int m1, int m2, int m3, int m4){
marks1=m1;
marks2=m2;
marks3=m3;
marks4=m4;
}
public float getPercentage(){
float total=((marks1+marks2+marks3+marks4)/(float)400)*100;
return total;
}
}

Q6. (a) /1

Checked Exception Unchecked Exception


It is a compile time exception. It is a runtime exception.
The compiler checks such type of The compiler does not check these types
exceptions. of exceptions.

NegativeValueException.java /3

class NegativeValueException extends Exception{


NegativeValueException(){
System.out.println("From const");
}
}

negativeno.java

import java.util.*;
class negativeno{
public static void main (String data[]){
Scanner sc= new Scanner(System.in);
try{
System.out.println("Enter an integer");
int n= sc.nextInt();
if(n<0)
throw new NegativeValueException();
}
catch(NegativeValueException e){
System.out.println("Caught "+ e);
}
finally{
System.out.println("Exception successfully handled");
}
}
}
Q6. (b)
To pass parameters from HTML page to Applet program, the <param> tag is used along with the
<applet> tag. The syntax for <param> tag is as follows -
/1

<html>
<body>
<applet code="test" width=700 height=400 >
<param name=user value="Mr. X"> </param>
</applet>
</body>
</html>
/3
test.java
import java.applet.*;
import java.awt.* ;

public class test extends Applet{


String name,msg;
Int rollno;
float cgpa;
public void init(){
msg="Student details are as follows ----";
name=getParameter("s_name");
rollno= getParameter("s_rollno");
cgpa=getParameter("s_cgpa");
}
public void start(){
//getParameter method can be called here.
}
public void paint(Graphics g){
g.drawString(msg,20,20); //coordinates are taken approximately
g.drawRect(20,40,300,200);
g.drawString(“Name : ”+name+“ Rollno : "+rollno+ “ CGPA :”+cgpa,30,60);
}
}
Q7(a)
Java throw keyword is used to throw an exception explicitly in
the code, inside the function or the block of code. A single
exception can be thrown using throw statement.

Java throws keyword is used in the method signature to declare


an exception which might be thrown by the function during the
execution of the code. Multiple exceptions can be declared using
throws statement.

Two approaches used to handle checked exceptions are :

I) wrapping the Java code which throws the checked exception


within a try catch block.
II) using the keyword throws to throw the checked exception up
the stack to the calling method to handle.

(b)
Two constructors of the PrintWriter class are :
(i) PrintWriter(OutputStream out, boolean autoFlush)
Creates a new PrintWriter from an existing OutputStream.
(ii) PrintWriter(String fileName)
Creates a new PrintWriter, without automatic line flushing, with
the specified file name.

Following is the Java program :

import java.io.*;
class FileToFileCopyDemo {

public static void main(String args[]) throws IOException


{
int i;
FileReader fr = null;
FileWriter fw = null;
BufferedReader br = null;
BufferedWriter bw = null;
try {
fr = new FileReader("abc.txt");
br = new BufferedReader(fr);
}
catch(FileNotFoundException e){
System.out.println("File abc.txt not found");
return;
}

try {
fw = new FileWriter("xyz.txt");
bw = new BufferedWriter(fw);

}
catch(FileNotFoundException e){
System.out.println("Error in opening file xyz.txt");
return;
}

try {
do{
i = br.read();
if(i!=-1)
{
bw.write((char)i);
}
}while(i!=-1);
}
catch(IOException e){
System.out.println("File Error");
}
finally {
try {
if (br != null)
br.close();
if (bw != null)
bw.close();
if (fr != null)
fr.close();
if (fw != null)
fw.close();

} catch (IOException ex) {


ex.printStackTrace();
}
}

}
}

Q8. a) In Java, an applet is a special type of program embedded in the web page to generate
dynamic content. Applet is a class in Java. The applet life cycle can be defined as the process
of how the object is created, started, stopped, and destroyed during the entire execution of its
application. It basically has five core methods namely init(), start(), stop(), paint() and
destroy().These methods are invoked by the browser to execute. Along with the browser, the
applet also works on the client side, thus having less processing time.

There are five methods of an applet life cycle, and they are:

o init(): The init() method is the first method to run that initializes the applet. It can be invoked
only once at the time of initialization. The web browser creates the initialized objects, i.e., the
web browser (after checking the security settings) runs the init() method within the applet.
o start(): The start() method contains the actual code of the applet and starts the applet. It is
invoked immediately after the init() method is invoked. Every time the browser is loaded or
refreshed, the start() method is invoked. It is also invoked whenever the applet is maximized,
restored, or moving from one tab to another in the browser. It is in an inactive state until the
init() method is invoked.
o stop(): The stop() method stops the execution of the applet. The stop () method is invoked
whenever the applet is stopped, minimized, or moving from one tab to another in the browser,
the stop() method is invoked. When we go back to that page, the start() method is invoked
again.
o destroy(): The destroy() method destroys the applet after its work is done. It is invoked when
the applet window is closed or when the tab containing the webpage is closed. It removes the
applet object from memory and is executed only once. We cannot start the applet once it is
destroyed.
o paint(): The paint() method belongs to the Graphics class in Java. It is used to draw shapes
like circle, square, trapezium, etc., in the applet. It is executed after the start() method and
when the browser or applet windows are resized.

Parameters Java Application Java Applet

Applications are just like a Java Applets are small Java programs that are
program that can be executed designed to be included with the HTML
Definition
independently without using the web document. They require a Java-
web browser. enabled web browser for execution.

The applet does not require the main()


main () The application program requires a
method for its execution instead init()
method main() method for its execution.
method is required.

The “javac” command is used to Applet programs are compiled with the
compile application programs, “javac” command and run using either the
Compilation
which are then executed using the “appletviewer” command or the web
“java” command. browser.

Java application programs have


Applets don’t have local disk and network
File access full access to the local file system
access.
and network.

Applications can access all kinds Applets can only access browser-specific
Access level of resources available on the services. They don’t have access to the
system. local system.

First and foremost, the installation


The Java applet does not need to be
Installation of a Java application on the local
installed beforehand.
computer is required.

Applications can execute the Applets cannot execute programs from the
Execution
programs from the local system. local machine.

An application program is needed


An applet program is needed to perform
Program to perform some tasks directly for
small tasks or part of them.
the user.

It cannot start on its own, but it can be


It cannot run on its own; it needs
Run executed using a Java-enabled web
JRE to execute.
browser.
Parameters Java Application Java Applet

Connection Connectivity with other servers is


It is unable to connect to other servers.
with servers possible.

Read and
It supports the reading and writing It does not support the reading and writing
Write
of files on the local computer. of files on the local computer.
Operation

Application can access the Executed in a more restricted environment


Security system’s data and resources with tighter security. They can only use
without any security limitations. services that are exclusive to their browser.

Java applications are self- Applet programs cannot run on their own,
Restrictions contained and require no additional necessitating the maximum level of
security because they are trusted. security.

b) We all know that string possesses immutable and fixed-length character, whereas string buffer
possesses the characters, such as increasable and the writable series. The advantage of string buffer is
that it automatically grows when there is a need to add characters and substrings in between or at the
bottom.
import java.util.*;
public class PalindromeUsingStringBuffer
{

public static void main(String[] arg)


{
PalindromeUsingStringBuffer rev=new PalindromeUsingStringBuffer();
Scanner sc=new Scanner(System.in);
System.out.print("Enter a string : ");
String s=sc.nextLine();
String rev1=rev.reverse(s);
if(s.equals(rev1))
{
System.out.println("Palindrome");
}
else
{
System.out.println("Not Palindrome");
}
}
//calling method
static String reverse(String str)
{
String rev="";
for(int i=str.length();i>0;--i)
{
rev=rev+(str.charAt(i-1));
}
return rev;

}
}

You might also like