• Share
  • Email
  • Embed
  • Like
  • Save
  • Private Content
OXUS 20 JAVA Programming Questions and Answers PART II
 

OXUS 20 JAVA Programming Questions and Answers PART II

on

  • 1,396 views

OXUS 20 is pleased to offer PART II of Java Programming Questions and Answers with details explanation to support the educational needs and hoping these series help and benefit Computer Science ...

OXUS 20 is pleased to offer PART II of Java Programming Questions and Answers with details explanation to support the educational needs and hoping these series help and benefit Computer Science student, IT professionals and those who eager to learn programming and in particular Java Programming.

Statistics

Views

Total Views
1,396
Views on SlideShare
1,396
Embed Views
0

Actions

Likes
6
Downloads
33
Comments
1

0 Embeds 0

No embeds

Accessibility

Upload Details

Uploaded via as Adobe PDF

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel

11 of 1 previous next

  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

    OXUS 20 JAVA Programming Questions and Answers PART II OXUS 20 JAVA Programming Questions and Answers PART II Document Transcript

    • 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 7
    • 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.  System.out.println(scores);  System.out.println(scores.toString()); One way is Using the static method Arrays.toString(array)  System.out.println(java.util.Arrays.toString(scores)); to get a string representation  System.out.println(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 7
    • https://www.facebook. com/Oxus20 6) Is 1.003 - 0.003 == 1.0?  There is no guarantee that 1.003 - 0.003 == 1.0 is true.  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 7
    • 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. }  The program has a compile error.  The program has a runtime error.  The program runs, but displays nothing.  The program runs and displays "It is even!" (Without quotes). Abdul Rahman Sherzad Page 4 of 7
    • 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); } }  counter is 5 isPrime is true  counter is 5 isPrime is false  counter 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 are non-primitive! Abdul Rahman Sherzad Page 5 of 7
    • 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 statement; condition check; 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 7
    • https://www.facebook. com/Oxus20 is a nonprofit society with the aim of changing education for the better by providing education and assistance to Computer Science and IT professionals. The society's goal is to provide information and assistance to Computer Science professionals, increase their job performance and overall agency function by providing cost effective products, services and educational opportunities. So the society believes with the education of Computer Science and IT they can help their nation to higher standards of living. Follow us on Facebook, https://www.facebook.com/Oxus20 Abdul Rahman Sherzad Page 7 of 7