Java Exam Questions Review
Java 2 - ﻣﺮاﺟﻌﺔ ﺷﺎﻣﻠﺔ
المحتويات
1
(Multiple Choice) أسئلة االختيار من متعدد:ً أوال
In Java, the method that transfers the thread from the running state to the time waiting :Q
state is:
a) Stop() b) Sleep() c) Terminate() d) Delay()
b) Sleep() :الجواب
ﺗﻘﻮم ﺑﻨﻘﻞ اﻟﺨﻴﻂ اﻟﺒﺮﻣﺠﻲ ﻣﻦ ﺣﺎﻟﺔ اﻟﺘﺸﻐﻴﻞ إﱃ ﺣﺎﻟﺔ اﻻﻧﺘﻈﺎر اﻟﻤﺆﻗﺖ ﻟﻔﺘﺮة زﻣﻨﻴﺔsleep() اﻟﺪاﻟﺔ:الشرح
. ﺗﻨﻘﻠﻪ إﱃ ﺣﺎﻟﺔ اﻻﻧﺘﻈﺎر اﻟﺪاﺋﻢwait() ﺑﻴﻨﻤﺎ،ﻣﺤﺪدة
The method length() of a File returns: :Q
a) Number of lines b) Number of bits c) Number of bytes d) Number of tokens
c) Number of bytes in a file :الجواب
. ﺗﻌﻴﺪ ﺣﺠﻢ اﻟﻤﻠﻒ ﺑﻮﺣﺪة اﻟﺒﺎﻳﺖFile ﻓﻲ ﻛﻼسlength() اﻟﺪاﻟﺔ:الشرح
We can extend String class. :Q
a) True b) False
b) False :الجواب
. وﺑﺎﻟﺘﺎﻟﻲ ﻻ ﻳﻤﻜﻦ ﻋﻤﻞ وراﺛﺔ ﻣﻨﻪ،final ﻣﻌﺮف ﺑﻜﻠﻤﺔString اﻟﻜﻼس:الشرح
What is the output of the following program? (Exception Handling flow where m1 prints :Q
’wish’, throws exception, catch prints ’luck’, finally prints ’in’, then ’exam’).
a) wish good luck in exam b) wish luck in exam c) luck in exam
b) wish luck in exam :الجواب
ﺛﻢ،luck اﻟﺨﻄﺄ وﻳﻄﺒﻊcatch ﻳﻤﺴﻚ،good ﻳﺤﺪث اﺳﺘﺜﻨﺎء ﻓﻴﺘﻢ ﺗﺠﺎوز،wish ﻓﺘﻄﺒﻊtry ﻳﺘﻢ ﺗﻨﻔﻴﺬ:الشرح
. وأﺧﻴﺮا ً ﻳﻜﻤﻞ اﻟﺒﺮﻧﺎﻣﺞ،in وﻳﻄﺒﻊfinally ﻳﻨﻔﺬ
The data fields in an interface are: :Q
a) static only b) final only d) both static and final
d) both static and final :الجواب
.public, static, final ً ( ﺗﻜﻮن ﺿﻤﻨﻴﺎInterface) اﻟﻤﺘﻐﻴﺮات داﺧﻞ اﻟﻮاﺟﻬﺔ:الشرح
Non-abstract classes are called: :Q
a) real classes b) instance classes d) concrete classes
2
d) concrete classes :الجواب
.Concrete Class اﻟﻜﻼس اﻟﺬي ﻻ ﻳﺤﺘﻮي ﻋﲆ دوال ﻣﺠﺮدة وﻳﻤﻜﻦ إﻧﺸﺎء ﻛﺎﺋﻨﺎت ﻣﻨﻪ ﻳﺴﻤﻰ:الشرح
What is the output of the Nested Try-Catch? (Prints V, try prints W, 9/0 error happens, :Q
catches print Y, then Z).
a) VWXYZ b) VWZ e) VWYZ
e) VWYZ :الجواب
ً ﻣﺒﺎﺷﺮة ﻣﺘﺠﺎوزاcatch ﻓﻴﻨﺘﻘﻞ إﱃ، ﺗﺘﻢ اﻟﻄﺒﺎﻋﺔ ﺑﺎﻟﺘﺴﻠﺴﻞ ﺣﺘﻰ ﺣﺪوث اﻟﺨﻄﺄ ﻋﻨﺪ اﻟﻘﺴﻤﺔ ﻋﲆ ﺻﻔﺮ:الشرح
.ﻣﺎ ﺑﻌﺪ اﻟﺨﻄﺄ
Which method is used to perform (insert, update, delete) statements in JDBC? :Q
a) executeResult() b) executeQuery() c) executeUpdate()
c) executeUpdate() :الجواب
ﻟﺠﻤﻞexecuteQuery() ﺑﻴﻨﻤﺎ، ﻟﺘﻨﻔﻴﺬ ﺟﻤﻞ اﻟﺘﻌﺪﻳﻞ واﻹﺿﺎﻓﺔ واﻟﺤﺬفexecuteUpdate() ﺗﺴﺘﺨﺪم:الشرح
.اﻻﺳﺘﻌﻼم
Interfaces can have constructors. :Q
a) True b) False
b) False :الجواب
. ﻷﻧﻪ ﻻ ﻳﻤﻜﻦ إﻧﺸﺎء ﻛﺎﺋﻨﺎت ﻣﻨﻬﺎ ﻣﺒﺎﺷﺮةConstructors اﻟﻮاﺟﻬﺎت ﻻ ﺗﺤﺘﻮي ﻋﲆ:الشرح
Which of these methods of ArrayList class is used to obtain current size of an object? :Q
a) size() b) length() c) index()
a) size() :الجواب
.length ﺑﻴﻨﻤﺎ اﻟﻤﺼﻔﻮﻓﺎت اﻟﻌﺎدﻳﺔ ﺗﺴﺘﺨﺪم اﻟﺨﺎﺻﻴﺔ،size() اﻟﺪاﻟﺔArrayList ﺗﺴﺘﺨﺪم:الشرح
Using JDBC API, when changing the underlying DBMS from MySQL to SQL Server, devel- :Q
opers do not have to change the code that accesses the database.
a) True b) False
a) True :الجواب
.( ﻓﻘﻂ دون ﺗﻐﻴﻴﺮ ﻛﻮد اﻟﺠﺎﻓﺎDriver) ﺣﻴﺚ ﻳﺘﻢ ﺗﻐﻴﻴﺮ اﻟﻤﺸﻐﻞ،JDBC ﻫﺬه ﻣﻴﺰة اﻟﺘﺠﺮﻳﺪ ﻓﻲ:الشرح
3
Which of these standard collection classes implements a dynamic array? :Q
a) AbstractList c) ArrayList
c) ArrayList :الجواب
. ﺗﻄﺒﻴﻘﺎ ً ﻟﻤﻔﻬﻮم اﻟﻤﺼﻔﻮﻓﺔ اﻟﺪﻳﻨﺎﻣﻴﻜﻴﺔArrayList ﺗﻌﺘﺒﺮ:الشرح
Which of the following exception is/are unchecked exception? :Q
a) ArithmeticException b) SQLException d) FileNotFoundException
a) ArithmeticException :الجواب
.Unchecked ﺗﻌﺘﺒﺮRuntimeException اﻻﺳﺘﺜﻨﺎءات اﻟﺘﻲ ﺗﺮث ﻣﻦ:الشرح
What is the output? (m3 throws, m2 catches & re-throws new exception, m1 catches). :Q
a) BAC b) BA c) B
b) BA :الجواب
. ﻟﻢ ﻳﺼﻞ إﻟﻴﻪ ﺧﻄﺄ ﻟﻴﻄﺒﻌﻪmain اﻟـ.m1 ﻓﺎﻟﺘﻘﻄﺘﻪ، ﻋﺎﻟﺠﺔ اﻟﺨﻄﺄ ﺟﺰﺋﻴﺎ ً وأﻋﺎدت رﻣﻴﻪm2 اﻟﺪاﻟﺔ:الشرح
Final methods support dynamic polymorphism. :Q
a) True b) False
b) False :الجواب
. ﻟﺬا ﻻ ﺗﺪﻋﻢ ﺗﻌﺪد اﻷﺷﻜﺎل اﻟﺪﻳﻨﺎﻣﻴﻜﻲ، ﻟﻬﺎOverride ( ﻻ ﻳﻤﻜﻦ ﻋﻤﻞfinal) اﻟﺪوال اﻟﻨﻬﺎﺋﻴﺔ:الشرح
Output of Constructors: Shape prints ”Base”, Circle extends Shape prints ”Derived”. :Q
a) Derived b) Base c) Base Derived
c) Base Derived :الجواب
. ﻳﺘﻢ اﺳﺘﺪﻋﺎء ُﻣﻨﺸﺊ اﻷب أوﻻ ً ﺛﻢ ُﻣﻨﺸﺊ اﻻﺑﻦ:الشرح
4
(Tracing & Debugging) تتبع الكود واألخطاء:ًثانيا
Tracing Q1: Polymorphism & Calculation :Q
ClassA: returns 9. ClassB (extends A): returns 9. ClassC (extends B): super * 2. ClassD
(extends A): super * 3.
9.0, 9.0, 18.0, 27.0 :الجواب
. ﻟﻠﺤﺼﻮل ﻋﲆ ﻗﻴﻤﺔ اﻷب اﻟﻤﺒﺎﺷﺮsuper ﻳﺘﻢ ﺣﺴﺎب اﻟﻘﻴﻢ ﺑﻨﺎًء ﻋﲆ اﻟﻮراﺛﺔ واﺳﺘﺪﻋﺎء:الشرح
Tracing Q2: Inheritance Fields :Q
MyCB extends MyCA. Output of printData() (x+y) and printInfo() (child method).
Output: ”2 1”, then Compilation Error :الجواب
أﻣﺎ اﻟﺜﺎﻧﻴﺔ ﻓﺨﻄﺄ ﻷﻧﻨﺎ ﻧﺴﺘﺨﺪم ﻣﺮﺟﻌﺎ ً ﻣﻦ ﻧﻮع اﻷب ﻻﺳﺘﺪﻋﺎء داﻟﺔ ﻣﻮﺟﻮدة ﻓﻘﻂ، اﻟﻄﺒﺎﻋﺔ اﻷوﱃ ﺻﺤﻴﺤﺔ:الشرح
.ﻓﻲ اﻻﺑﻦ
Common Compilation Errors :Q
Various Errors :الجواب
:الشرح
. اﻟﻤﺘﻐﻴﺮات اﻻﻓﺘﺮاﺿﻴﺔ ﻻ ﻳﻤﻜﻦ اﻟﻮﺻﻮل إﻟﻴﻬﺎ ﻣﻦ ﺣﺰﻣﺔ أﺧﺮى:Default Access .1
. ﻻ ﻳﻤﻜﻦ اﺳﺘﺪﻋﺎء داﻟﺔ ﻋﺎدﻳﺔ ﻣﻦ ﻛﻼس آﺧﺮ دون إﻧﺸﺎء ﻛﺎﺋﻦ:Method Call .2
.abstract ﻳﺠﺐ ﺗﻌﺮﻳﻒ اﻟﻜﻼس ﻛـ، إذا اﺣﺘﻮى اﻟﻜﻼس ﻋﲆ داﻟﺔ ﻣﺠﺮدة:Abstract Method .3
. ﻻ ﻳﻤﻜﻦ إﺳﻨﺎد ﻛﺎﺋﻦ اﻷب إﱃ ﻣﺮﺟﻊ اﻻﺑﻦ دون ﺗﺤﻮﻳﻞ ﺻﺮﻳﺢ:Downcasting .4
Array List Removal Logic :Q
Removing element at index vs Object, and removing inside a loop.
Logic Check :الجواب
( أو اﻟﺘﻜﺮارi–) ﻳﺠﺐ إﻧﻘﺎص اﻟﻌﺪاد، ﻓﻲ اﻟﺤﻠﻘﺎت اﻟﺘﻜﺮارﻳﺔ. ﺗﻨﺰاح اﻟﻌﻨﺎﺻﺮ اﻟﺘﺎﻟﻴﺔ، ﻋﻨﺪ ﺣﺬف ﻋﻨﺼﺮ:الشرح
.اﻟﻌﻜﺴﻲ ﻟﻀﻤﺎن ﻓﺤﺺ ﺟﻤﻴﻊ اﻟﻌﻨﺎﺻﺮ
Polymorphism & Constructors :Q
Class A constructor calls setI(20). Class B overrides setI to multiply by 3. new B() called.
i from A is 60 :الجواب
Overridden) ﻳﺘﻢ ﺗﻨﻔﻴﺬ ﻧﺴﺨﺔ اﻟﺪاﻟﺔ اﻟﺨﺎﺻﺔ ﺑﺎﻻﺑﻦ، ﺣﺘﻰ داﺧﻞ ُﻣﻨﺸﺊ اﻷب، ﺑﺴﺒﺐ ﺗﻌﺪد اﻷﺷﻜﺎل:الشرح
.(Method
5
(Coding Solutions) األسئلة البرمجية:ًثالثا
1. ArrayList & instanceof :Q
Complete code to create Student/Person objects, add to list, and print details.
} ( list < Person > ArrayList ) printList void static public 1
} ( list : p Person ) for 2
} ( Student instanceof p ) if 3
; p ( Student ) = s Student 4
( ) getID . s + " : ID " + ( ) getName . s + " : Student " ) println . out . System 5
;(
} else { 6
; ( ( ) getName . p + " : Person " ) println . out . System 7
{ 8
{ 9
{ 10
2. JDBC Connection & Select :Q
Complete code to connect to SQLite and display employees.
} ( ) openConnection void public 1
} try 2
; ( " JDBC . sqlite . org " ) forName . Class 3
; ( " db . CompanyDB \ \ : D : sqlite : jdbc " ) getConnection . DriverManager = conn 4
{ ; ( ) printStackTrace . e } ( e Exception ) catch { 5
{ 6
7
} ( ) displayEmployees void 8
} try 9
; ( ) createStatement . conn = stmt Statement 10
; ( " Employees FROM * SELECT " ) executeQuery . stmt = rs ResultSet 11
} ( ( ) next . rs ) while 12
" ) getString . rs + " t \ " + ( " empID " ) getInt . rs ) println . out . System 13
; ( ( " empName
{ 14
{ ; ( ) printStackTrace . e } ( e SQLException ) catch { 15
{ 16
3. Threading Search :Q
Create a Runnable class to search for a number in an array.
} Runnable implements SearchArray class public 1
; n int ; a [ ] int 2
{ ; n = n . this ; a = a . this } ( n int , a [ ] int ) SearchArray public 3
4
Override @ 5
} ( ) run void public 6
} ( + + i ; length . a > i ; 0 = i int ) for 7
} ( n = = [ i ] a ) if 8
; ( i + " index at " + n + " Found " ) println . out . System 9
; return 10
{ 11
6
{ 12
; ( " . found not " + n ) println . out . System 13
{ 14
{ 15
; ( ( 5 , arr ) SearchArray new ) execute . executor : main in Usage / / 16