0% found this document useful (0 votes)
25 views8 pages

Practice Questions JAVA

The document contains a comprehensive set of questions and programming tasks related to Java, covering topics such as exception handling, Java Beans, multithreading, JDBC, and file handling. It includes practical programming exercises using Swing components, JSP, and object-oriented programming concepts. Additionally, it addresses string manipulation, constructors, access specifiers, and various Java programming concepts with example solutions.

Uploaded by

Usha Sharma
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views8 pages

Practice Questions JAVA

The document contains a comprehensive set of questions and programming tasks related to Java, covering topics such as exception handling, Java Beans, multithreading, JDBC, and file handling. It includes practical programming exercises using Swing components, JSP, and object-oriented programming concepts. Additionally, it addresses string manipulation, constructors, access specifiers, and various Java programming concepts with example solutions.

Uploaded by

Usha Sharma
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

Especially FOR JAVA (EFJ) Complied By ARJUN BHAT

1.) What are exceptions? Why is it important to handle exceptions ?


Discuss different keywords that are used to handle exception.
(2+2+6)
2.) Write a program using swing components to add two numbers. Use
text fields for inputs and output. Your program should display the
result when the user presses a button. (10)
3.) What is java beans? Differentiate it with java class. Discuss bean
writing process with suitable examples. (2+2+6)
4.) Discuss the use of interfaces to achieve multiple inheritance. (5)
5.) Discuss the use of multithreading with suitable example. (5
6.) What is JDBC? How do you execute SQL statements in JDBC? (2+3)
7.) Discuss any five classes to handle files in java. (5)
8.) How can you handle events using adapter classes? Discuss. (5)
9.) Write a simple JSP program to display “Kathmandu, Nepal” 10
times. (5)
10.) What is interface? How can you use the concept of interface to
achieve multiple inheritance? Discuss with suitable example.
11.) Write a program using swing component to multiply two numbers.
Use text fields for input and output. Your program should display
the result when the user press a button.
12.) Write a simple java program to read from and write to files
13.) Why multithreading is important in programming? Discuss
14.) iscuss the role of event listeners to handle events with suitable
example
15.) Discuss any 5 exception classes in java.
16.) Write a program using swing components to find simple interest.
Use text fields for inputs and output. Your program should display
the result when the user presses a button. (10)
17.) How do you achieve multiple inheritance in java? Discuss. (5)
18.) Write a Java program using JDBC to extract name of those students
who live in Kathmandu district, assuming that the student table has
four attributes (ID, name, district, and age).
19.) Write an object oriented program in Java to find area of circle
20.) Write a simple Java program that reads data from one file and
writes the data to another file.
21.) Write the simple java program that reads data from one file and
writes data to another file
22.) Discuss the process of sending email messages using Java
23.) List the types of operator in Java? Explain any three of them
24.) How static and final keywords are used in Java?Explain the inner
class with suitable example
25.) Differentiate between package and interface with suitable example.
Especially FOR JAVA (EFJ) Complied By ARJUN BHAT

26.) Differentiate between error and exception ? How exceptions can be


handled in java?
27.) What is constructor? Differentiate between method overriding and
function overloading?
28.) What do you mean by the scope of variable? Describe the role of
public, private protected and default access specifier used in java
program?
29.) Write a Java Program to develop simple calculator for addition
purpose only. User can enter different type of numbers or different
orders for number to find out ‘sum’. Do it using method
overloading?
30.) Create a class Employee with instance variable name, address
telephone, monthly basic salary and allowance. Write default and
parameterized constructors. Also write a method to calculate to
income tax of employee assuming 15 tax on earning greater than 2
lakhs.
31.) What is a Constructor? What are its special properties ? Explain the
different levels of access protection available in Java?

1. How to compare strings? Use “==” or use equals()?


In brief, “==” tests if references are equal and equals() tests if values are
equal. Unless you want to check if two strings are the same object, you
should always use equals().

It would be better if you know the concept of string interning.


2. Why is char[] preferred over String for security sensitive
information?
Strings are immutable, which means once they are created, they will stay
unchanged until Garbage Collector kicks in. With an array, you can explicitly
change its elements. In this way, security sensitive information(e.g.
password) will not be present anywhere in the system.
3. Can we use string for switch statement?
Yes to version 7. From JDK 7, we can use string as switch condition. Before
version 6, we can not use string as switch condition.
// java 7 only!
Especially FOR JAVA (EFJ) Complied By ARJUN BHAT

switch (str.toLowerCase()) {
case "a":
value = 1;
break;
case "b":
value = 2;
break;
}
4. How to convert string to int?
int n = Integer.parseInt("10");
Simple, but so frequently used and sometimes ignored.
5. How to split a string with white space characters?
We can simple do split using regular expression. “\s” stands for white space
characters such as ” “, “\t”, “\r”, “\n”.
String[] strArray = aString.split("\\s+");
6. What substring() method does?
In JDK 6, the substring() method gives a window to an array of chars which
represents the existing String, but do not create a new one. To create a new
array to back string, you can do add an empty string like the following:
str.substring(m, n) + ""
This will create a new char array that represents the new string. The above
approach sometimes can make your code faster, because Garbage Collector
can collect the unused large string and keep only the sub string.
In Oracle JDK 7, substring() creates a new char array, not uses the existing
one. Check out the diagram for showing substring() difference between JDK 6
and JDK 7.
7. String vs StringBuilder vs StringBuffer
String vs StringBuilder: StringBuilder is mutable, which means you can
modify it after its creation.
StringBuilder vs StringBuffer: StringBuffer is synchronized, which means it is
thread-safe but slower than StringBuilder.
Especially FOR JAVA (EFJ) Complied By ARJUN BHAT

8. How to repeat a string?


In Python, we can just multiple a number to repeat a string. In Java, we can
use the repeat() method of StringUtils from Apache Commons Lang package.
String str = "abcd";
String repeated = StringUtils.repeat(str,3);
//abcdabcdabcd
9. How to convert string to date?
String str = "Sep 17, 2013";
Date date = new SimpleDateFormat("MMMM d, yy",
Locale.ENGLISH).parse(str);
System.out.println(date);
//Tue Sep 17 00:00:00 EDT 2013

10. How to count # of occurrences of a character in a string?


Use StringUtils from apache commons lang.
int n = StringUtils.countMatches("11112222", "1");
System.out.println(n);
Especially FOR JAVA (EFJ) Complied By ARJUN BHAT

Practice Questions & SOLUTIONS

1. Write a Java application which prompts the user to enter 15 integers,


then computes the sum, and then prints the sum to the screen.
import java.util.Scanner;
public class SumInts{
public static void main (String[] args){
Scanner scan = new Scanner(System.in);
int sum=0;
System.out.print("Enter 15 integers: ");
for (int i=1; i<=15; i++)
sum += scan.nextInt();
System.out.println("The sum is " + sum);
}
}

2. Recall the Pythagorean Theorem says that if a; b are the lengths of two
edges of a right triangle, then the length of the hypotenuse c is given
by
c2 = a 2 + b 2:
Write a Java application which prompts the user to enter the lengths
of two edges of a right triangle and then computes the length of the
hypotenuse.
Solution:
import java.util.Scanner;
public class Hypotenuse{
public static void main(String[] args){
Scanner scan = new Scanner(System.in);
double a,b,c;
System.out.print("Enter the lengths of two sides: ");
a = scan.nextDouble();
b = scan.nextDouble();
c = Math.sqrt(a*a + b*b);
System.out.println("The hypotenuse has length " + c);
}
}

3. What is the output of the following code fragment?


int x = 1;
while (x<5){
System.out.println(x);
if (x==3)
x++;
else
x = x+2;
Especially FOR JAVA (EFJ) Complied By ARJUN BHAT

}
Solution:
1
3
4

4. What is the output of the following code fragment?


for (int j=1; j<=5; j++){
for (int i=1; i<=4; i++)
System.out.print("x");
System.out.println();
}
Solution:
xxxx
xxxx
xxxx
xxxx
xxxx

5. Which of the following are valid Java identi_ers?


XBox
$3t4
hop-toad
if
2ndFloor
MyGoodness
Solution: XBox, $3t4, and MyGoodness are valid Java identi_ers.
hop-toad is not, since the - character is not valid (it is treated as a
minus sign). if is not valid, since it is a Java reserved word. 2ndFloor
is not valid since it begins with a digit.

6. What is the output?


public class EnumerateSeasons{
enum Season{Spring,Summer,Fall,Winter}
public static void main(String[] args){
Season s = Season.Spring, t = Season.Winter;
System.out.println(s.name());
System.out.println(t.ordinal());
}
}
Solution:
Spring
3

7. The Java compiler produces


(a) Java bytecode
Especially FOR JAVA (EFJ) Complied By ARJUN BHAT

(b) machine language


(c) assembly language
(d) an iterated list
(e) an html _le
Solution: (a)

8. A byte is
(a) 256 bits
(b) 4 bits
(c) 8 bits
(d) approximately 1000000 bits
(e) 1/8 of a bit
Solution: (c)

9. The Java expression


5/2+3
evaluates to
(a) 1
(b) 5
(c) 5.5
(d) 0
(e) none of the above
Solution: (b). Evaluate using integer division 5/2+3 is 2+3 is 5.

10. Which are the following is part of the hardware of a computer system?
(a) operating system
(b) compiler
(c) interpreter
(d) bus
(e) bytecode
Solution: (d)

11. Write a Java expression which represents the oating-point constant


two thirds.
Solution: 2./3 or (double)2/3 or 2.0/3.0

12. If x and y are Java variables of type double, write the Java code for
the mathematical expression
x2 + 2
3y 􀀀 5
Solution: (Math.pow(x,2) + 2) / (3*y - 5)

13. Write a Java application which rolls two standard dice (generates two
random integers from 1 to 6), and prints out their sum.
Solution:
public class TwoDice{
Especially FOR JAVA (EFJ) Complied By ARJUN BHAT

public static void main (String[] args){


int die1,die2,sum;
die1 = (int)(Math.random()*6) + 1;
die2 = (int)(Math.random()*6) + 1;
sum = die1 + die2;
System.out.println("The sum of the two dice is " + sum);
}
}

14. For each of the following pairs, which represents a class and which
represents an object of that class?
(a) Celebrity, Britney Spears
Solution: class: Celebrity, object: Britney Spears
(b) Mickey Mouse, Rodent
Solution: class: Rodent, object: Mickey Mouse
(c) Author, William Shakespeare
Solution: class: Author, object: William Shakespeare
(d) Sport, Baseball
Solution: class: Sport, object: Baseball

15. Assume n is a Java variable of type int. Write a code fragment to print
to the screen positive if n is positive, zero if n is zero, and negative
if n is negative.
Solution:
if (n>0)
System.out.println("positive");
else if (n==0)
System.out.println("zero");
else
System.out.println("negative");

16. Compute the Java expression 15%6 + 9.


Solution: 15%6 + 9 is 3 + 9 is 12.

You might also like