0 ratings0% found this document useful (0 votes) 35 views9 pagesProgramming-2 Sheets
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content,
claim it here.
Available Formats
Download as PDF or read online on Scribd
See:
A GridLayout B. FlowLayout C. _ BorderLayout)p,
21. When a Java application starts, what 1s the name of the method that 1s executed?
QQ _maim B) os Cc) begin D) Main,
2 TeatencTosed between this and the end of line ts ignored by the compiler
one
Cc) 0 mn
2S Writes themame of the file declaring the class namedMyClass?
A) MyClass ©) MyClass.class
B) myelass java D) MyClass java >
i __ MyClass
24. Ain) _ is acollection of code that has been progi2mmed and translated by someone else, ready for
use in your program.
A) method ©) rameter
B) class Dy library >
25 pen a moran consists of one or more of these fundamental building blocks.
7 cla c) applet
B) CPU D) parameter
26. What is the name of the file that contains the Java source code for the class BankAccount?
A) BankAccount c) BankAccount.class
a Bank Account java_) D) BankAccount.txt
ad Contains sequences of instructions to perform a particular task.
A) parameter 6) variable
B) label
28. The Java compiler ignores any text between
A and * ©) {* and *}
Gt D) Mand |) or
29. What term is used to refer to a sequence of characters enclosed in quotation marks? 52
inh oO comment ae
le
© he (eee)
B Object D) vari
30. What is the output of the following Java statement? "System. out printIn("
A 10 c. 4
D
Bas
31. What is the output of the following Java statement? System.out printin(4 + 6);
A. 4+6
B 4 D. 46
32. Which statement is true about a Java program?
A) Java forces the programmer to use a particular layout for readability. »
B) Java requires that at most one statement appear on one line. &
Cs ‘The first method that is executed in a Java program is called Main. *
Cee
33. The Java comiptler translates source code into what type of file?
A) document
B) object D) text
34. Identify and correct re errors in each of the following statements:
1. if(e<7, VERES
System.out. ody "cis less than 7" ),
2. if(e=27) 1FUC >= F)
System.out printin( "c is equal to or greater than 7" ),
‘write a complete program that calculates and prints the product of three integers. (Note by scanner or
JOptionPane classes). ‘
36. How many times does the following loop execute? int i= 0,
>
CamScanner + Lia > g.unallCamScanner 4 Lis &sucall‘
Dt
on the following code, which
suse the 2 code, which of the follo
200 wing statements sets the frame to a width of 400 and a
setVisible();
IGHT = 200; JFrame frame =
(FRAME _WIDTH, FRAME. HIGHT) «
AME WIDTH FRAM
D. frame setSize(FRAME_HE
36. Eas “ie cae statement, which of the following statements sets the ile ofthe fame SFra.
A. frame title = ""An Empty Frame’
B. frame setTitle(JFrame EMPTY),
_C. frame addTitle("" An Empty Frame’
D._ frame setTitle(”*An Empty Frame);
37, What is the nickname for the graphical user interface library in Java?
rw. Applet _C. JComponent
B. GUI CD. Swing
38. What does an object store its data in? —————
A. files CC. instance vari
ifiers
B. methods D. accessspeci
39. Which one of the following statements can be used to convert a string str to a double”
a) double n = str.parseDouble();*
b) double n = Integer. parseDouble(str);
@ double n = Double parseDouble(str);
d) double n = double parseDouble(str); 2 3
40. What is the value of Math.pow(2, 3)?
a)50 (os)
b)60 90
41. Given two method definitions, tell which of the two methods is invoked for:
#1: public static double m(double x, double y)
#2: public static double m(int x, double y)
double z= m(4, 4.5); Zz
double z = m(5, 4); Zz
(
1
double z= m(4.5, 5.4),
double z= m(4.5, 4):
42, Show the output of the following program
public class Test {
public static void maualsiringil args), {
int number = — Pre mihi ve
int (J numbers = new int [1]; —» reherenc-e
m(number, numbers);
System.out.printin("number is "+ number +" and numbers|0] is"+ —numbers|0|);
}
public static void m(int x, int [] y) {
output x=12; — y[0]=13; xt,
ih
number's 0 and numbers] 15
CamScanner 4 Lis & sucall|
45, What are the features of an Object driented Programming
A) Inheritance
B) Encapsulation
44, What are the feature
'A) Methods
B) Variables
45, The class that is
A) Subclass
46,
es reused using Inheritance in Java?
being inherited or su
ss that inherits an alread
B) Superclass
447, Java language supports
le Inheritance
in
type of inheritance
y defined class is called _
i (OOP)?
C) Polymorphism
D) All the above
€) Constants
D) All the above ) \
classed is called
Conall (3 aie JIB
eS
class A {
public AQ) {
System.out printin(
"The default constructor of A is invoked"),
He
class B extends A {
public BO { super )s
System. out printin(
The default constructor of B is invoked");
y
public class C {
public static void main(String] ares) {
Bb=new BO;
}
|
public static void main(Stringt]
public class Test {
args)\
Aa=new AQ;
a.p(10);
a.p(10.0),
Hs
class B {
public void p(double i) {
‘System out printIn(i *2);
Mb
class AextendsB{ overt oe
public void p(double i) {
System.out.println(i);
}
class Animal {
String name = "animal",
String makeNoise()
{ return "generic noise", } }
class Dog extends Animal{
String name = "dog",
String makeNoise()
{ return "bark"; } }
public class Test {
public static void main(String|] args) {
‘Animal an = new Dog(),
.+""-+an. makeNoise());
7
class Test {
private static int x; = °
private int y=2;
public Test(){
xt
yrh }
public static void main(String{]
args){ 3
Test ol =new Test(); “=! 19°
Test 02 =new Test); **2 -3 3
System out printin("y= "+02."
x= "+02.x);
‘System.out. printIn(an.name
jin Lb h
‘49. Complete the following sentences:
1. Keyword--CL E14 requests memory from the system to store an object, then calls the corresponding
class’s constructor to initialize the object.
2, Methods——————
sysiem-ow- print Yo
sy Sem. out - Prete
ere na
display information in a commani
\d window.
Lc
CamScanner 4 Lis a suueall| Name: dec:
42, Which of the following constructs a Circle of radius 3, assuming the construction parameter is the rag,
value?
A. Circle(3).new C. new Cirele(3)
CB__new Circle(3) > D. Circle(3)
43. Which statement declares a variable that references a Circle of radius 3?
A. Circle c= Circle(3) C. Circle ¢.new.Ci
D. ¢.Circle(3);
“44, Which siatement calls a constructor with no construction parameters?
C. Circle ¢ = new Circle;
3);
B.A call to a constructor must haye construction D. Circle ¢ = Circte()
parameters,
45, What terminology describes a method that returns information about its implicit parameter sniioes yet
change theparameter’s internal data? ger mahed
A_mutator C. void
D. public
46. What terminology describes a method that modifies the | data of its implicit parameter?
A. public C. mutator) sey wrenno
B. void ‘accessor 4
20.4 is a collection of classes with a related purpose.
C. method
B. import D. collection
28. To use a class in another package you need to__it
A. export C._tewrite
B. overload
29. Which package is automatically imported in any Java program?
A. java system C. javalanguage
D. java.util
30. Which class is part of the java.lang package?
A. Rectangle CC String ) System
B. PrintStream D. Cirele
31. Assuming the following Java statement.
Circle cl = new Circle(3); What does the variable cl store?
‘A. The constructed object itself.
B_ A reference to the Circle cl
C_ A reference to the memory location of the constructed obj
D. The numeric value 3 ane. 4
32. What is the output of the sannR Er C2-rodtus = iy
Circle cl =new Circle(3); Circle e2= el; cl.setRadius(4), or
ystem out printin(c2.getRadius()); ae
C6 Ww?
ee ee
33. What is the output of the following code:Cirele cl = new Circle(3),
Circle c2 = new Circle(3); cl setRadius(4); System.out printin(c2.getRadius());
C6
. 8 D4
34. Complete this code fragment to ensure that the frame is shown:
came frame = new JFrame();
(© tame sible) Sac mean
CamScanner 4 Lis & sucall=Set:
sna variable x declared as a double and initialized, show the
. shovy the java code to cast and store :
ble y which is defined as a short, oo Lott er yw tS ohana Ng algo x
nto.a va
Write a java statement can be used to convert a string str to a double?
4
S. What is the statement to display the message “Hello world" in a message dialog box? |
6. Convert the following java statement to Conditional Operator (?:) :
if (x > 0) 7 4 ‘
ye (x0) L ;
else a Tats
enh SphonPane «show! z %
7. What does the following state . snt do? for (; ) {do something; .
8. To returns the square root of 2 ».sing method V5. or
9. To rounded up the number to iis nearest integer using method |
10. The output from invoking Math.random() is_MNcarn yah
11. The default value ofa data field is_\)|__ for a reference type
Codes
(UZ Show one line of Java code that declares and initializes an array of type integer with exactly 4 elements
nap dol G
whose values are 97, 33, 44, and 12, in order. —>
Write a method that retums the sum of all the elements in a specified column in a matrix using the
following header: public static doublesumColumn(double{][] m, intcolumnindex) Write 2 test program
that reads a 3-by-4 matrix and displays the sum of each column
1, Find the index of the smallest element) Write a method that retums the index of the smallest element in
an array of integess. If the number of such elements is greater than 1, return the smallest index. Use the
following header: public static int indexOfSmallestElement(double{] array) Write a test program that
prompts the user to enter ten numbers, invokes this method to retur the index of the smallest clement,
and displays the index
he area of a rectangle by creating a class named ‘Area’ taking the values
( Write a program to print tl
ceadth as parameters of its constructor and having a'method named ‘tetumArea’ which
of its length and breadth
returns the area of the rectangle. Length and breadth of rectangle are entered through keyboard
J Create a class named Person that holds the following fields: two String for the person's first and last
name and a LocalDate object for the person’s birthdate. Create a class named Couple that contains two
‘ors for each class that accept parameters for each field and provide get
passes each to a method
Person objects. Provide construct
methods for each field. Then write a program that creates two objects and in turn
that displays all the details.
(Employee Class) Create a class called Employee that includes three instance variables—a first name
(type String), a last name (type String) and a monthly salary (double). Provide a constructor that
initializes the three instance variables. Provide a set and a get method for each instance variable. If the
monthly salary is not positive, do not set its value. Write a test application named Employee Test that
demonstrates class Employee's capabilities. Create two Employee objects and display each object’s
yearly salary. Then give each Employee a 10% raise and display each Employee's yearly salary again
A ‘A palindrome is a sequence of characters that reads the same backward as forward. For example, each of
the following five-digit integers is a palindrome: 12321, 55555, 45554 and 11611. Write an application
that reads in a five-digit integer and determines whether it’s a palindrome, If the number is not five digits
ong, display an error message and allow the user to enter a new value.
8. Write a Java program to display the employee id, age, first name and last name using JDBC connectivity.
7
CamScanner 4 Lis a suueallSec =
The program connects to 4 local MySQL database and displays all elements in which salary m
nore A
than 100008
6 Waite java method for find summation of ragged array and max number
(JZ lava Program to Find Area of Square, Rectangle and Circle using Method Overloading
IZ Write a recursive method for computing a factorial factorialin), given n. The test program prompts the
Name:
user fo enter n
Implement a class named person and two subclasses of Person named Student and Employee. Mak
o Faculty and Staff subviasses of Employee. A person has a name, address, phone number, and ‘aul
address. A student has « class status (freshman, sophomore, junior, or senior). Define the status as a
constant. An employee has an office, salary, and date-hired. A faculty member has office hours and a
rank. A staff member has a title. Override the toString method in each class to display the class name and
the person's name, [hint: After implementation Draw the UML]
Wal 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.
Simple Interest = (P x R x T)/100_ where P = Principal Amount, R = Rate of interest, T = Time (years)
ter of a JPanel The innermost circle should have a radius of 10
id have a radius 10 pixels larger than the previous one. Begin by
UZ Draw 12 concentric circles in the cen
ircle, move up one radius and to the left
pixels, and each successive circle shoul
finding the center of the JPanel. To get the upper-left comer of a ci
‘one radius from the center, The width and height of the bounding rectangle are both the same
circle’s diameter (i.e., twice the radius).
16. Write a Java Student class which has three private data members name. id, and score. The student's name
‘The score is an integer between 0 and 100. Then
as the
and id are all strings of no more than 10 characters.
constructor and getter/setter for each data member. finally, method for display the highest score and the
lowest score. Write UML for student class in the } inst page
IW
UX
CamScanner 4 Lis a suueall