What is the output of this program? (or, can it be compiled)?



Download 1,04 Mb.
bet1/4
Sana22.06.2021
Hajmi1,04 Mb.
#73073
  1   2   3   4
Bog'liq
BasicJavaTask5 homework


Basic Java Task 5

  1. What is the output of this program? (or, can it be compiled)?

class Test {

public static void main(String[] args) {

System.out.println(f((int)5));

}

public static String f(String n) {



System.out.print("string ");

return n;

}

public static long f(long n) {



System.out.print("long ");

return n;

}

}



Ans: The output of this program is: long 5.


  1. The following JavaFX layout is best implemented using?





  1. What exception will arise from this following code, explain it!

System.out.println(Byte.parseByte("123456789"));
Ans: There is a number format Exception and also the value is out of range.


  1. What does the following code print? Explain it!

int x = 5;

for(int i = 0; i < 10; i++) {

switch(i%3) {

case 0:


x += 2;

default:


x += 1;

}

}



System.out.println(x);

Ans: The output of above code is given below:





  1. What does the following code print? Explain it!

int i = 0;

int j = 20;

try {

j = i/2;


i = (j+1)/j+1;

} catch(ArithmeticException e) {

i = 30;

j = 50;


} catch(Exception e) {

i = 8;


j = 9;

}

finally {



i -= 10;

j += 10;


}

System.out.println(i + j);



Ans: The output of the above program is given below:


  1. Given the definitions of class A, class B, and interface C as follows. Will everything be compiled successfully? Choose your answer.

a. C c = new C(); // yes /no [Ans: yes]

b. C c = new A(); // yes /no [ Ans: yes]

c. C c = new B(); // yes /no [Ans: no]





interface C { }

class A implements C { }

class B { }




  1. Given this code, what will be return value of h (41)? Explain it.




public static int h(int i) {

return i<=1?1:i-h(i/2);

}

Ans:

The return value of h (41) is: 27.










  1. What is information hiding in Java? Give an example!


Ans:

  • In programming, the process of hiding details of an object or function. Information hiding is a powerful programming technique because it reduces complexity. One of the chief mechanisms for hiding information is encapsulation -- combining elements to create a larger entity. The programmer can then focus on the new object without worrying about the hidden details. In a sense, the entire hierarchy of programming languages, from machine languages to high-level languages, can be seen as a form of information hiding.

  • Information hiding is also used to prevent programmers from intentionally or unintentionally changing parts of a program.

  • Encapsulation in Java is a mechanism of wrapping the data (variables) and code acting on the data (methods) together as a single unit. In encapsulation, the variables of a class will be hidden from other classes, and can be accessed only through the methods of their current class. Therefore, it is also known as data hiding.

  • To achieve encapsulation in Java :

-Declare the variables of a class as private.


-Provide public setter and getter methods to modify and view the variables values.



  1. What does the following code print? Assume the “File.txt” is not available. Explain it.

try {

try (BufferedReader br = new BufferedReader(new FileReader("File.txt"))) {

System.out.print("1");

}

}



catch(ArithmeticException e) {

System.out.print("2");

}

catch(IOException e) {



System.out.print("3");

}

finally {



System.out.print("4");

}


Download 1,04 Mb.

Do'stlaringiz bilan baham:
  1   2   3   4




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©hozir.org 2024
ma'muriyatiga murojaat qiling

kiriting | ro'yxatdan o'tish
    Bosh sahifa
юртда тантана
Боғда битган
Бугун юртда
Эшитганлар жилманглар
Эшитмадим деманглар
битган бодомлар
Yangiariq tumani
qitish marakazi
Raqamli texnologiyalar
ilishida muhokamadan
tasdiqqa tavsiya
tavsiya etilgan
iqtisodiyot kafedrasi
steiermarkischen landesregierung
asarlaringizni yuboring
o'zingizning asarlaringizni
Iltimos faqat
faqat o'zingizning
steierm rkischen
landesregierung fachabteilung
rkischen landesregierung
hamshira loyihasi
loyihasi mavsum
faolyatining oqibatlari
asosiy adabiyotlar
fakulteti ahborot
ahborot havfsizligi
havfsizligi kafedrasi
fanidan bo’yicha
fakulteti iqtisodiyot
boshqaruv fakulteti
chiqarishda boshqaruv
ishlab chiqarishda
iqtisodiyot fakultet
multiservis tarmoqlari
fanidan asosiy
Uzbek fanidan
mavzulari potok
asosidagi multiservis
'aliyyil a'ziym
billahil 'aliyyil
illaa billahil
quvvata illaa
falah' deganida
Kompyuter savodxonligi
bo’yicha mustaqil
'alal falah'
Hayya 'alal
'alas soloh
Hayya 'alas
mavsum boyicha


yuklab olish