Download as pdf or txt
Download as pdf or txt
You are on page 1of 11

Electrical Engineering Dept.

Programming Languages III – Java Control & Computer Section


Final Exam 2020/2021 – 4th year - 2nd Term
‫عبدالرحمن الغشيمي‬

1) What is the output of the below Java program with FOR loop?

for(int j=0; j<5;j++;)


System.out.print(j + ",");
options:
A) 1,2,3,4, B) 0,1,2,3,4 C) Compiler error. D) None

2)What is the output of the below Java program with two classes?

//Testing1.java
public class Example{ }
public class Testing1
{
public static void main(String[] args) {
System.out.println("Hello Boss.!") }
options:
Hello Boss.! B) No Output C) Compiler error. D) None of the above

3) What is the output of the below Java program?

//bingo.java file

Public class Hello


public static void main(String[] args)
{
System.out.println("BINGO");}
}
options:
A) bingo . B) BINGO C) Compiler error D) None

4 ) What is the output of this program?


class array_output {
public static void main(String args[])
{
char array_variable [] = new char[10];
for (int i = 0; i < 10; ++i)
{
array_variable[i] = 'i';
System.out.print(array_variable[i] + "");
} } }

A) 1 2 3 4 5 6 7 8 9 10 C) i j k l m n o p q r

B) 0 1 2 3 4 5 6 7 8 9 10 D) i i i i i i i i i i

1/6
Electrical Engineering Dept.
Programming Languages III – Java Control & Computer Section
Final Exam 2020/2021 – 4th year - 2nd Term
‫عبدالرحمن الغشيمي‬

5) What is the output of the below Java program?

public class TestingMethods5 {


public static void main(String[] args) { i
int localVariable;
System.out.println(localVariable); } }
Options:-
A) 0 B ) garbage value. C) NullPointerException D) Compiler error

6) What is the output of this program?


class array_output
{
public static void main(String args[])
{
int array_variable [] = new int[10];
for (int i = 0; i < 10; ++i)
{
array_variable[i] = i;
System.out.print(array_variable[i] + " ");
i++; } } }
A) 0 2 4 6 8 B) 1 3 5 7 9
C) 0 1 2 3 4 5 6 7 8 9 D) 1 2 3 4 5 6 7 8 9 10

7) Which of these is an incorrect Statement?


A) It is necessary to use new operator to initialize an array
B) Array can be initialized using comma separated expressions surrounded by curly braces
C) Array can be initialized when they are declared
D) None of the mentioned

8) Which of these is necessary to specify at time of array initialization?

A) Row. B) Column. C) Both Row and Column D)None of the mentioned

9) What is the type of variable ‘b’ and ‘d’ in the below snippet?
int a[], b;
int []c, d;
options:
A) b’ and ‘d’ are int. B) b’ and ‘d’ are arrays of type int
C) ‘b’ is int variable; ‘d’ is int array D) ‘d’ is int variable; ‘b’ is int array
10) a class can extends --------- class(es) , An interface can extends ------
-- interface(es)
Options:-
A) single/single B)single/multiple C) multiple/single D) multiple/multiple

2/6
Electrical Engineering Dept.
Programming Languages III – Java Control & Computer Section
Final Exam 2020/2021 – 4th year - 2nd Term
‫عبدالرحمن الغشيمي‬

11) What is the output of the below Java program that passes an object to
another method?
class Food
{
int items; int show()
{return items;}
}
class Testing
{
public static void main(String[] args)
{
Food f = new Food(); f.items = 5;
System.out.println("Items Before = " + f.show()); change(f);
System.out.println("Items After = " + f.show()) } static void change(Food
foo)
{ foo.items = 10; }
A) B)
Items Before = 10 Items Before = 5
Items After = 10 Items After = 5 .
C) D)
Items Before = 5 Items Before = 10
Items After = 10 Items After = 5

12) What is the output of the Java program with Enhanced FOR loop below?

String countries[] = {"BRAZIL", "CHILE", "SYDNEY"};


int i=0;
for(String str: countries) {
if(i<2) ;
else break;
System.out.print(str + ","); i++; }

A) BRAZIL,CHILE,SYDNEY, B) BRAZIL,CHILE,

C) BRAZIL, D) Compiler error

3/6
Electrical Engineering Dept.
Programming Languages III – Java Control & Computer Section
Final Exam 2020/2021 – 4th year - 2nd Term
‫عبدالرحمن الغشيمي‬

13-What is the output of the below Java program with a final local variable?
public class TestingMethods8
{ int cars = 20;
void change(final int cars) {
cars = 10;
this.cars = cars; }
public static void main(String[] args) {
TestingMethods8 t8 = new TestingMethods8();
t8.change(30);
System.out.println(t8.cars); } }

Options:-
A) 30 B) 20 C) 10 D ) Compiler error

14) What is the output of the below Java program with arrays?

string[] ary = {"KITE", "AIR"};


String str = "PLANE";
ary[1] = str;
str = "FLY";
System.out.println(ary[1]);
A) AIR B) PLANE
C) FLY D) Compiler error

15) Which of these is an incorrect array declaration?

A) int arr[] = new int[5] ; B) int [] arr = new int[5] ;


C) int arr[]; arr = new int[5]; D) d) int arr[] = int [5] new;

16) What is the process of defining a method in a subclass having same name &
type signature as a method in its superclass?
A) Method overloading B) Method overriding
C) Method hiding D) None of the mentioned

17) Which of these keywords can be used to prevent Method overriding?


A) Static B) constant C) protected D) final

18) Which of these is correct way of calling a constructor having no parameters,


of superclass A by subclass B?

A) super(void) . B) superclass () .
C) super A() . D) super() .

4/6
Electrical Engineering Dept.
Programming Languages III – Java Control & Computer Section
Final Exam 2020/2021 – 4th year - 2nd Term
‫عبدالرحمن الغشيمي‬

19) What is the output of this program?

class comma_operator
{
public static void main(String args[])
{
int sum = 0;
for (int i = 0, j = 0; i < 5 & j < 5; ++i, j = i + 1)
sum += i;
System.out.println(sum);
}}
options:
A) 5 B) 6 C)14 D) compilation error

20) What is the output of this program?


class evaluate
{
public static void main(String args[])
{
int arr[] = new int[] {0 , 1, 2, 3, 4, 5, 6, 7, 8, 9};
int n = 6;
n = arr[arr[n] / 2]; System.out.println(arr[n] / 2)}
}
A) 3 B) 0 C) 6 D)1

21) What is the output of this program?


class array_output
{
public static void main(String args[])
{
int array_variable[][] = {{ 1, 2, 3}, { 4 , 5, 6}, { 7, 8, 9}};
int sum = 0;
for (int i = 0; i < 3; ++i)
for (int j = 0; j < 3 ; ++j)
sum = sum + array_variable[i][j];
System.out.print(sum / 5);}}

A) 8 B) 9 C) 10 D)11

5/6
Electrical Engineering Dept.
Programming Languages III – Java Control & Computer Section
Final Exam 2020/2021 – 4th year - 2nd Term
‫عبدالرحمن الغشيمي‬

22) What is the output of this program?


class multidimention_array {
public static void main(String args[])
{
int arr[][] = new int[3][];
arr[0] = new int[1];
arr[1] = new int[2];
arr[2] = new int[3];
int sum = 0;
for (int i = 0; i < 3; ++i)
for (int j = 0; j < i + 1; ++j)
arr[i][j] = j + 1;
for (int i = 0; i < 3; ++i)
for (int j = 0; j < i + 1; ++j)
sum + = arr[i][j];
System.out.print(sum); }}
Options:-
A) 11 B) 10 C) 13 D)14

23) What will be the output of the following Java code?

int arr[] = new int [5];


System.out.print(arr);
A) Garbage value B) b) value stored in arr[0]. C) 00000 D) 0

24) java class can contain___.

a) Variables B) Methods, Constructors


c) Inner Classes (A class inside another class) D) All the above

25) A constructor can call another overloaded constructor using the ___ keyword in Java.

A) this B) local C) con D) super

26) ---------------- is the superclass to all Java classes either user-defined or built-in.
A) Class B) Object c) Superclass D) Null

27) a class can extends --------- class(es) , An interface can implements --------
interface(es)
A)single/sinle B)single/multiple C) multiple/single D) multiple/multiple

28) ‫ خيار واثنين أسئلة مباشرة فقط يعني هناك سؤال واحد فقط لم استطع تذكره‬28 ‫لكن التقلق عدد أسئلة الخيارات هي‬ ‫لم استطع تذكره اظن انه سؤال كان لمصفوفة ذات بعدين‬

6/6
Electrical Engineering Dept.
Programming Languages III – Java Control & Computer Section
Final Exam 2020/2021 – 4th year - 2nd Term
‫عبدالرحمن الغشيمي‬

29) Find, enumerate, and explain the errors in the following program:(the line help you
explain)

public class Echo {

public static void main(String[] args) {


String[2] arr = {"hellow,there", "how are you? "};
print(arr); }

void print(String[] greetings) {

for (int i = 0; i <= args.length; i++)

System.on.print(greetings[i] + " ");

return true; }

1--------------------------------------------------------------------------------------------------------------------------------------------------
2--------------------------------------------------------------------------------------------------------------------------------------------------
3--------------------------------------------------------------------------------------------------------------------------------------------------
4--------------------------------------------------------------------------------------------------------------------------------------------------
5--------------------------------------------------------------------------------------------------------------------------------------------------
6--------------------------------------------------------------------------------------------------------------------------------------------------
7--------------------------------------------------------------------------------------------------------------------------------------------------
8--------------------------------------------------------------------------------------------------------------------------------------------------
9--------------------------------------------------------------------------------------------------------------------------------------------------

30- Write a Java method intSumDigit that take in an integer n and returns the sum
of all its digits.
(for instance (1234)<..1+2+3+4..>=12)

‫ﺍﻷﺳﺌﻠﺔاالخرين في نفس دفتر األسئلة لذلك كان من‬ ‫طبعا كان إجابة ﺟﻤﻴﻊ‬
‫السؤالين‬
‫ وهذة األسئلة الذي هي جميع أسئلة‬. ‫المستحيل ان نخرج ورقة األسئلة‬
‫االختباراال سؤال انما حاولت استذكرها وقمت بتجميعها وترتيبها ومن ثم‬
.‫أمال ان تنتفعوا بذلك‬ .‫حلها كامال في النهاية‬

‫لم يكن الهدف ابدا ان تحفظوا األسئلة النه ببساطة نحن ندرس لنتعلم وأيضا‬
‫ربما لن يكرر الدكتور أي سؤال من االختبار لهذا العام‬
‫ انما كان بمثابة مساعدة لك لتقيس مدى فهمك وتذاكر اكثر و اكثر وتكون‬.
‫ وهللا الموفق‬.‫مطمئنا عندما ترى بساطة األسئلة‬

7/6
Electrical Engineering Dept.
Programming Languages III – Java Control & Computer Section
Final Exam 2020/2021 – 4th year - 2nd Term
‫عبدالرحمن الغشيمي‬

1 (A) A) 1,2,3,4,
--------------------------------------------------------
2 (C) Compiler error ( ‫ في نفس الملف‬class ‫( الكثر من‬public ) ‫نالحظ وجود‬
‫ ( داخل ملف واحد في الجافا‬public class) ‫رغم انه ال يمكن انشاء اكثر‬
--------------------------------------------------------
3 (C) Compiler error (class) ‫يجب ان يكون اسم الملف للجافا مثل اسم‬
------------------------------------------
4 (D) i i i i i i i i i i
--------------------------------------------
5 (D) Compiler error
--------------------------------------------------------
6 (A) 0 2 4 6 8
--------------------------------------------------------
7 (A) It is necessary to use new operator to initialize an array ‫اإلجابة الخطأ من الخيارات هي‬
--------------------------------------------------------
8 (A) Row.
--------------------------------------------------------
9 (C) ‘b’ is int variable; ‘d’ is int array
--------------------------------------------------------
10 (B) single/multiple
--------------------------------------------------------
11 (C)Items Before = 5 Items After = 10
--------------------------------------------------------
12 (B) BRAZIL,CHILE,
--------------------------------------------------------
13 (D) Compiler error( ‫ تسبب حدوث خطأ‬cars *= 10; ‫ بعد إعطائه قيمة لذلك‬final ‫ال يمكن تغيير قيمة المتغير من النوع‬

--------------------------------------------------------
14 (B) PLANE
--------------------------------------------------------
15 (D) int arr[] = int [5] new
--------------------------------------------------------
16(B) Method overriding

8/6
‫‪Electrical Engineering Dept.‬‬
‫‪Programming Languages III – Java‬‬ ‫‪Control & Computer Section‬‬
‫‪Final Exam‬‬ ‫‪2020/2021 – 4th year - 2nd Term‬‬
‫عبدالرحمن الغشيمي‬

‫‪--------------------------------------------------------‬‬
‫‪17(D) final‬‬
‫‪--------------------------------------------------------‬‬

‫;)(‪18(D) super‬‬
‫‪--------------------------------------------------------‬‬
‫‪19(B) 6‬‬
‫‪--------------------------------------------------------‬‬
‫‪20(D) 1‬‬
‫‪--------------------------------------------------------‬‬
‫‪21(B) 9‬‬
‫‪--------------------------------------------------------‬‬
‫‪22(B) 10‬‬
‫‪--------------------------------------------------------‬‬
‫‪23(A) Garbage value‬‬
‫‪--------------------------------------------------------‬‬
‫‪24(D) All the above‬‬
‫‪--------------------------------------------------------‬‬
‫‪25(A) this‬‬
‫‪--------------------------------------------------------‬‬
‫‪26( B) Object‬‬
‫‪--------------------------------------------------------‬‬
‫‪27(B) Single/multiple‬‬
‫‪--------------------------------------------------------‬‬
‫) (‪28‬‬
‫كما قلت مسبقا عدد الخيارات ‪ 28‬هناك خيار لألسف لم استطع تذكره اظن كان السؤال لمصفوفة ذات بعدين‬
‫ابضا الخيار رقم ‪ 23 & 1‬ال أتذكر جيدا انه من ضمن الخيارات او‬
‫كانت أسئلة اخرى‬

‫؏‬
‫*‬
‫؏‬
‫تجميع وحلول‬
‫عبدالرحمن الغشيمي‬
‫؏‬ ‫‪2021/8/24‬‬

‫؏‬
‫*‬

‫؏‬ ‫‪9/6‬‬
Electrical Engineering Dept.
Programming Languages III – Java Control & Computer Section
Final Exam 2020/2021 – 4th year - 2nd Term
‫عبدالرحمن الغشيمي‬

public class Echo {

public static void main(String[] args) {


[] ‫ال يمكن ان يحتوي‬
string[2] arr = {"hellow,there", "how are you? "}; ‫على أي قيمة‬ ‫تبع تعريف المصفوفة‬

print(arr); } ‫او يجب‬ ‫ال يمكن استدعاء الدالة بشكل مباشر اذا لم تكن‬
‫ ومن ثم استدعائها باستخدام‬class echo ‫ من‬object ‫انشاء‬
void print(String[] greetings) {
dot opretor
for (int i = 0; i <= args.length; i++) Greeting < args ‫نستبدل‬
=‫ونحذف إشارة المساواة‬
System.on.print(greetings[i] + " ");
out ‫ب‬on ‫نستبدل‬

return true; } return ‫لذاك ال يمكن ات تحتوي على‬void ‫الدالة من النوع‬

------------------------------------------------------------

public class Echo { ‫بعد التصحيح‬


public static void main(String[] args) {

String[] arr = {"hellow,there", "how are you? "};


print(arr); }

static void print(String[] greetings) {

for (int i = 0; i < greetings.length; i++)

System.out.print(greetings[i] + " ");

}}

10 /
6
Electrical Engineering Dept.
Programming Languages III – Java Control & Computer Section
Final Exam 2020/2021 – 4th year - 2nd Term
‫عبدالرحمن الغشيمي‬

30 ‫اجابة السؤال األخير‬

‫بالنسبة لهذه الخطوة باإلمكان عمل‬


‫الشرط‬
while(n != 0)
‫اذا كان المستخدم يريد معرفة إشارة‬
‫)له‬digit(‫الرقم الذي تم جمع الرقم‬

// 2021//2022// Abdurrahman Alghushaimi


package Exam2021;

import java.util.Scanner;
public class SumDigit
{
static int intSumDigit(int n){
int sum=0;
while(n ! 0) {
sum += n % 10;
n /= 10; }//end while loop
return sum; }//end function intSumDigit
public static void main(String args[])
{
int number;
‫أخيرا نرجوا منكم‬
System.out.println("please inter integer number"); ‫المسامحة اين‬
Scanner input = new Scanner(System.in); ‫وجدتم أخطاء‬
number=input.nextInt(); ‫فالكمال هلل وحده‬
System.out.println( "sum of digit for "+number +" is " +intSumDigit(number));
}}

‫مالحظات يجب عليك العيش مع المادة اكثر مدة ممكنة اجعل عندك الشغف لتعلمها فهي‬
.‫حقا ممتعة‬
.string ‫بالنسبة ألسئلة االختبار الدكتور اكثر تركيزه على المصفوفات ثم‬
‫ باإلمكان البحث عن هذه األسئلة لمعرفة‬.‫اطلعوا على تمارين من المواقع المشهورة‬
. ‫المواقع‬

‫و التنسونا من دعوة صادقة‬ ‫فاهلل الموفق‬ ‫وقبل كل شيء استعينوا باهلل‬


11 /
6 ؏

You might also like