0% found this document useful (0 votes)
30 views11 pages

Final Java 2023

Uploaded by

amrt6958
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
30 views11 pages

Final Java 2023

Uploaded by

amrt6958
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 11

Java Final Exam(3/6/2023)

Dr. Eman Meslhy


Q1[25M] Choose :-
1. Each class declaration that begins with keyword …….. must be stored in a file that has exactly
the same name as the class and ends with the .java filename extension .
a. static b. public static c. public d. default

2. An instance variable is known throughout the entire ………. .


a. method b. package c. class d. main

3. Keyword …… requests memory from the system to store an object, then calls the
corresponding class’s constructor to initialize the object.
a. class b. import c. new d. none of these

4. [int a=0; int x= ++a;] a and x are equal to ……………… .


a. 1, 1 b. 2, 2 c. 1, 2 d. 2, 1

5. Class Integer in …………….. .


a. Java.swing b. java.awt c. java.util d. java.lang

6. int a = 90;
switch(a){
case 80:
case 90: System.out.println(90 +“ ”);
default: System.out.println(“default ”); }
a. default b. 90 c. 80 90 default d. 90 default
7. If x is 1, what is x after this expression (1<x)&(x<100)?
a. true b. false c. 100 d. 1

8. How many times does the following code snippet display “Loop Execution”? for(int i=0;
i<10; i++);{ System.out.println(“Loop Execution”);}
a. 9 b. 10 c. 11 d. 1

9. Which of these classes is the super of all java classes ……………. .


a. Object b. System c. java d. class

10. showMessageDialog fourth argument is ………… .


a. string to display b. icon c. null d. String in title bar

11. double d=Math.round(2.5+Math.Random()); d is = ………… .


a. 2.0 b. 2.5 c. 3.0 d. 2

12. What is the value of s2 after the following code executes?


String s1 = “Hey”; String s2 = s1.subString(0,2);
a. Hey b. H c. ey d. He

13. double value = Math.random(); int num = (int) (value * 5) + 5;


num is a random number from ………………….. .
a. 0 to 4 b. 1 to 5 c. 5 to 9 d. 5 to 10
14. Variable sum = ………….. .
int[][] m = { {1,1,1,1}, {1,2,3,4}, {2,2,2,2}, {2,4,6,8} };
int sum = 0;
for(int k = 0; k< m.length ; k++){
sum = sum + m[m.length-1-k][1];}
a. 4 b. 9 c. 6 d. 10
15. When you create an array of objects, each reference is assigned the value
……….. .
a. True b. \u0000 c. null d. false
16. Both JTextField and JTextAreas directly extend class ……………. .
a. JComponent b. JTextComponent c. JtextArea d. Jtext
17. What is abstract in Java?
a. Keyword b. class c. method d. All of these
18. Which of the following methods is used to set the size of a component?
a. setBounds() b. setSize() c. setMinimumSize() d. setLayout()
19.List p = new ArrayList(); p.add(1); p.add(7); p.add(1); p.remove(1);
System.out.println(p);
a. [1,7] b. [7,1] c. [1,1] d. [1,7,1]
20. A Java Class inherits Constants and Methods of an Interface using ……..
Keyword .
a. interface b. extends c. implements d. all of these
21. All Interface methods in Java are ……… by default.
a. public b .abstract c. public and abstract d. default
22. When you implement a method that is also defined in a superclass, you
……. the original method.
a. copy b. call c. overload d. override

23. Invoking Class.forname method may throw ………… .


a. RuntimeException b. ClassNotFoundException c. IOException
24. Which of the following components can display images?
a. JTextArea b. JTable c. Jlist d. Jlabel

25. Which of the following events is fired when a button is clicked?


a. mousePressed b. mouseReleased c. actionpreformed d.keyPressed
Q2[15M] Answer the following questions:-
1. [4M]Write a typical Java program that shows the steps to access a student
name that has math degree more than 90 from faculty database.
2. [7M]Write a GUI java program login page that takes username and
password as shown in figure 1 note that :
• If we enter username as “Eman” and password “12345” with doctor radio
button OR If we enter username as “Adam” and password “67890” with
Student radio button and click on the LOGIN button then a message dialog
box should appear and display the message “Login Successful” and if we
enter different username or password then message dialog box should
display the message “Invalid Username or Password”. For message dialog
box we will use JOptionPane class.
• If we click on the RESET button, then all the texts of JTextField and
JpasswordField should be cleared.
• If we click on the showPassword JcheckBox, then the secret password
should be revealed, and if we uncheck the showPassword JcheckBox, then
all the texts of the JpasswordField should be masked with “*”characters.
3. [4M]Write a java code segment to draw a black circle has a polygon at the
coordinates as shown in figure 2, then resize the polygon into 5 scales of x-
axis and y-axis.
Q3[20M] Answer the following questions:-
1. [8M] Create a UML Class diagram for next mentioned company’s class
hierarchy maintaining all mentioned relationships. And Write JAVA program
for implementing next system and create necessary objects in main() method
to show their respective operations.
• Let us consider a Company class with properties of name, reg_no and type
and the function elements are getter and setter methods for each data
field. It has a parameterized constructor to initialize the name reg_no and
type at the time of object creation. Company class also have a display
method to show the company details.
• Company has a large number of employees of two different categories.
Employee class has emp_id, emp_name, designation as data fields and
getter and setter methods for these data fields. It defines a default
constructor. Employee class has also a payment method but has no
implementation.
• SalariedEmployee is one type of Employee with the basic properties of
Employee with additional properties of salaryScale. It has method to set
salaryScale and it override the payment method from the Employee class
which returns the salary of the employee.
• HourlyEmployee is another category or Employee which inherits the
Employee class and have the data member of hours_worked and
hourly_payment. It has getter and setter methods for data fields and it
implements the payment method for salary payment returning
hours_worked * hourly_payment.
2. [4M] What is the output of the following ?
A. class Base { B. class NewClass{
void demo(int a) { public static void main(String args[]){
System.out.println("a: "+a); } mystery(12);
void demo(int a, int b) { }
System.out.println("a and b: "+a +","+b ); } public static void mystery(int n){
double demo(double a) { if( n <= 1)
System.out.println("double a: "+ --a); System.out.println(n);
return a * a; } } else
class Test { {
public static void main(String args[]) { System.out.println(n+",");
Base Obj = new Base(); mystery(n/2);
double result; System.out.println(", "+n);
Obj.demo(10); Obj.demo(10, 20); }
result = Obj.demo(5.5); }
System.out.println("O/P : "+ ++result ); } } }
3. [4M] describe the error and rewrite the correct code:-
A. interface Car B. class ABC{
{ public void myMethod(){
int basePrice = 1000; System.out.println("Overridden
} method");
class InterfaceTest2 implements Car }}
{ class Demo extends ABC{
void changePrice(){ public void myMethod(){
basePrice = 2000; super.myMethod();
System.out.println(basePrice ); System.out.println("Overridden
} method");
} }
public static void main(String args[]) public static void main(String args[]){
{ Demo obj1 = new ABC();
new InterfaceTest2().changePrice(); ABC obj2 = new Demo();
} Obj1.myMethod();
Obj2.myMethod();
}}
4. [4M] add method collapse to the arraylist class that collapses the contents
of the arraylist of integers by replacing each successive pairs of integers with
the sum of the pair. If the list stores odd number of elements, the final
element should not be collapsed.
Example before collapsed Example after collapsed
1,2 ,3,4 ,5 3, 7, 5

You might also like