JAVA Programming Questions and Answers PART II


Published on

PART II of Java Programming Questions and Answers consists of different categories with correct answers with detailed explanation and reasoning as well as alternative solutions.
The aim of OXUS20 is to support and assist the IT and computer science professionals.

  • Be the first to comment

  • Be the first to like this

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

JAVA Programming Questions and Answers PART II

  1. 1. https://www.facebook. com/Oxus20 PART I – Single and Multiple choices, True/False and Blanks: 1) Which of the following correctly initializes an array myArray to contain four elements each with value 0? Why default 0 for int[] myArray = new int[4];?  int[] myArray = { 0, 0, 0, 0 }; In Java Language Specification the Default / Initial Value for any Object (class variable,  int[] myArray = new int[4]; instance variable, or array component) can be   For type byte, short and int the default value int[] myArray = new int[4]; for (int i = 0; i < myArray.length; i++) { given as Follows: is zero.  For type long, the default value is zero, that is, 0L.  For type float, the default value is positive myArray[i] = 0; zero, that is, 0.0f.  For type double, the default value is positive } zero, that is, 0.0d.  All of the above  For type char, the default value is the null character, that is, 'u0000'.  For type boolean, the default value is false.  For all reference types, the default value is null. 2) What will be the result of compiling following code? public class MainTest { public static void main(String args[]) { System.out.println("OXUS 20"); } The Java programming language supports overloading methods, and Java can distinguish between methods with different method signatures. public static void main(char args[]) { System.out.println("Tricky!"); } } Two of the components of a method declaration comprise the method signature — the method's name and the parameter types.  Code will not compile and will give "Duplicate main () method declaration" error  Code will compile correctly but will give a runtime exception  Code will compile correctly and will print "OXUS 20" (Without quotes) when it is run  Code will compile correctly and will print "Tricky!" (Without quotes) when it is run Abdul Rahman Sherzad Page 1 of 6
  2. 2. https://www.facebook. com/Oxus20 3) What is y displayed in the following code?  y is 3 - public class Test { public static void main(String[] args) { Because of followings rules and procedures: int x = 1; int y = x++ + 1 int y = x++ + x; x; 2 x is initially 1, and x++ is still 1 because System.out.println("y is " + y); } it is post increment; thus x will be 2 afterwards! }  y is 1  y is 2  y is 3  y is 4 4) Assume int[] scores = {65, 75, 70, 90, 85}; which of the following statement displays all the element values in the array? There are a multiple ways to print arrays in Java.  Sys intln(scores );  Sys intln(scores .toString()); One way is Using the static method Arrays.toString(array)  Sys intln(java.util.A rrays .toString(s cores )); to get a string representation  Sys intln(scores [0]); of one dimensional arrays. 5) In Arrays, variables of type ................... can be used as an array indexes.  short, long, String  short, byte, char  long, boolean, byte The Java specification limits arrays to at most Integer.MAX_VALUE elements. While a List may contain more elements! Abdul Rahman Sherzad // Invalid long[] primes_2 = new long[Long.MAX_VALUE];  int, long, short Long[] primes_1 = new Long[Long.MAX_VALUE]; // Invalid long[] primes_3 = new long[Integer.MAX_VALUE]; // Valid Page 2 of 6
  3. 3. https://www.facebook. com/Oxus20 6) Is 1.003 - 0.003 == 1.0?  Th e r e i s n o g u a r a n t e e t h a t 1 . 0 0 3 - 0 . 0 0 3 = = 1 . 0 i s t r u e .  true When comparing floating-point numbers (float, double) in Java, we quickly discover that we get round-off errors. This has to do with the limited precision of Java floating point variables. The following code example shows the problem at hand:  false public static void main(String[] args) { double num1 = 1.003; double num2 = 0.003; System.out.println( num1 - num2 ); 1.003 – 0.003 = 0.9999999999999999 } BUT public static void main(String[] args) { double num1 = 1.004; double num2 = 0.004; System.out.println( num1 - num2 ); 1.004 – 0.004 = 1.0 } Comparing floating points: • You should rarely use the equality operator (==) when comparing two floating point values (float or double) • Two floating point values are equal only if their underlying binary representations match exactly • In many situations, you might consider two floating point numbers to be "close enough" even if they aren't exactly equal To determine the equality of two doubles or floats, use the following technique: public static void main(String[] args) { double a = 1.003; double b = 0.003; double c = a - b; if (Math.abs(c - 1.0) <= 0.000001) { System.out.println("Equal"); Equal } else { System.out.println("Not Equal"); } } Abdul Rahman Sherzad Page 3 of 6
  4. 4. https://www.facebook. com/Oxus20 7) Programming style is important, because ______________.  A program may not compile if it has a bad style Good style is about making your program clear and understandable as well as easily modifiable.  Good programming style can make a program run faster  Good programming style makes a program more readable  Good programming style helps reduce programming errors 8) What is the output of the following code? public class Test { public static void main(String[] args) { int x = 0;  x is 4 while (x < 4) { - Because when x reach at 4 the while condition becomes false. x = x + 1; } Note that the print statement is outside of the while loop! System.out.println("x is " + x); } }  x is 1  x is 2  x is 3  x is 4 9) Analyze the following code: public class Test { public static void main(String[] args) { boolean even = false; if (even = true) { Please note assignment operator (=) used instead of equality (==) the following portion of the code: if (even = true) System.out.println("It is even!"); } } We assign true to the variable even, and thus, even variable is always true. Therefore, the statement "It is even!" will be printed out. }  Th e p r o g r a m h a s a c o m p i l e e r r o r .  Th e p r o g r a m h a s a r u n t i m e e r r o r .  Th e p r o g r a m r u n s , b u t d i s p l a y s n o t h i n g .  Th e p r o g r a m r u n s a n d d i s p l a y s " I t i s e ve n ! " ( W i t h o u t q u o t e s ) . Abdul Rahman Sherzad Page 4 of 6
  5. 5. https://www.facebook. com/Oxus20 10) What is the printout after the following loop terminates? public class Test { public static void main(String[] args) { int number = 25; int counter; boolean isPrime = true; for (counter = 2; counter < number && isPrime; counter++) { if (number % counter == 0) { isPrime = false; } } System.out.println("counter is " + counter + " isPrime is " + isPrime); } }  c ounter is 5 isPrime is true  c ounter is 5 isPrime is fals e  c ounter is 6 isPrime is true  counter is 6 isPrime is false Consider the following segment of the code: if (number % counter == 0) { isPrime = false; } When counter has the value of 5 the above condition evaluated to true since 25 % 5 equals 0. Please note that we are inside the loop and at the end the counter++ will be executed and then the condition of the loop will be evaluated to false and the loop will be terminated while counter has the value of 6. 11) ............. Which of the following is an example of a primitive data type?  Integer It's a coding convention, adopted by most Java programs  String that first letter of a Java classes be Capital. Therefore,  Scanner considering the conventions and standards all the options  Random Abdul Rahman Sherzad are non-primitive! Page 5 of 6
  6. 6. https://www.facebook. com/Oxus20 12) Is the following loop correct? public class YouBetterTest { public static void main(String[] args) { for (; ; ); } }  Yes  No Above for loop is the same as following program: for (; ; ) { } A for loop in java has the following structure:- for (initialization s tatement; condition chec k; increment) loop body; As you can see, there are four statements in the for loop structure as follow: Initialization statement: This statement is executed only once, when the loop is entered for the first time. This is an optional statement, meaning you can choose keep this field blank.  Conditional check: This statement is probably the most important one. It checks to verify whether or not certain expression evaluates to true. If it is, then the loop execution continues. You can choose to keep this field empty, which will be evaluated to true.  Increment / Decrement: This statement is used to increment / decrement some variable.  Loop body: The body of the loop, which will be executed again and again based on the conditional check's truth value. All in all, Now about the for (; ; ); syntax. It has no initialization statement, so nothing will be executed. Its conditional check statement is also empty, so which means it evaluates to true. After that the loop body is executed. Next, since the incremental statement is empty, nothing is executed. Then the conditional check is performed again which will again evaluates to true and then this whole process will again repeat. Abdul Rahman Sherzad Page 6 of 6
  7. 7. ‫‪OXUS20 is a non-profit society with the aim of changing education for‬‬ ‫‪the better by providing education and assistance to IT and computer‬‬ ‫.‪science professionals‬‬ ‫آکسیوس20 یک انجمن غیر انتفاعی با هدف تغییرو تقویت آموزش و پرورش از طریق ارائه آموزش و کمک به‬ ‫متخصصان علوم کامپیوتر است.‬ ‫نام این انجمن برگرفته از دریای آمو پر آب ترین رود آسیای میانه که دست موج آن بیانگر استعداد های نهفته این انجمن بوده، و این دریا از‬ ‫دامنه های کهن پامیر سرچشمه گرفته و ماورای شمالی سرزمین باستانی افغانستان را آبیاری میکند که این حالت دریا هدف ارایه خدمات‬ ‫انجمن را انعکاس میدهد.‬ ‫آکسیوس20 در نظر دارد تا خدماتی مانند ایجاد فضای مناسب برای تجلی استعدادها و برانگیختن خالقیت و شکوفایی علمی محصالن و دانش‬ ‫پژوهان، نهادمند ساختن فعالیت های فوق برنامه علمی و کاربردی، شناسایی افراد خالق، نخبه و عالقمند و بهره گیری از مشارکت آنها در‬ ‫ارتقای فضای علمی پوهنتون ها و دیگر مراکز آموزشی و جامعه همچون تولید و انتشار نشریات علمی تخصصی علوم عصری و تکنالوژی‬ ‫معلوماتی را به شکل موثر آن در جامعه در راستای هرچه بهتر شدن زمینه تدریس، تحقیق و پژوهش و راه های رسیدن به آمال افراد جامعه‬ ‫را فراهم میسازد.‬ ‫,‪Follow us on Facebook‬‬ ‫02‪‬‬