Professional Documents
Culture Documents
It-2004 (WT) - CS End May 2023
It-2004 (WT) - CS End May 2023
(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);
(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.
(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.*;
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
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.
2. CSS Id Selector
<!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>
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>
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>
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.
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 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
/** 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
}
}
}
super(20);
}
public static void main(String[] args) {
}}
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
NegativeValueException.java /3
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.* ;
(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.
import java.io.*;
class FileToFileCopyDemo {
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();
}
}
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.
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 “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.
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.
Applications can execute the Applets cannot execute programs from the
Execution
programs from the local system. local machine.
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
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
{
}
}