SlideShare una empresa de Scribd logo
1 de 7
Descargar para leer sin conexión
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

Más contenido relacionado

Más de Abdul Rahman Sherzad

Sorting Alpha Numeric Data in MySQL
Sorting Alpha Numeric Data in MySQLSorting Alpha Numeric Data in MySQL
Sorting Alpha Numeric Data in MySQLAbdul Rahman Sherzad
 
Cross Join Example and Applications
Cross Join Example and ApplicationsCross Join Example and Applications
Cross Join Example and ApplicationsAbdul Rahman Sherzad
 
Applicability of Educational Data Mining in Afghanistan: Opportunities and Ch...
Applicability of Educational Data Mining in Afghanistan: Opportunities and Ch...Applicability of Educational Data Mining in Afghanistan: Opportunities and Ch...
Applicability of Educational Data Mining in Afghanistan: Opportunities and Ch...Abdul Rahman Sherzad
 
Web Application Security and Awareness
Web Application Security and AwarenessWeb Application Security and Awareness
Web Application Security and AwarenessAbdul Rahman Sherzad
 
Database Automation with MySQL Triggers and Event Schedulers
Database Automation with MySQL Triggers and Event SchedulersDatabase Automation with MySQL Triggers and Event Schedulers
Database Automation with MySQL Triggers and Event SchedulersAbdul Rahman Sherzad
 
Evaluation of Existing Web Structure of Afghan Universities
Evaluation of Existing Web Structure of Afghan UniversitiesEvaluation of Existing Web Structure of Afghan Universities
Evaluation of Existing Web Structure of Afghan UniversitiesAbdul Rahman Sherzad
 
PHP Basic and Fundamental Questions and Answers with Detail Explanation
PHP Basic and Fundamental Questions and Answers with Detail ExplanationPHP Basic and Fundamental Questions and Answers with Detail Explanation
PHP Basic and Fundamental Questions and Answers with Detail ExplanationAbdul Rahman Sherzad
 
Fundamentals of Database Systems Questions and Answers
Fundamentals of Database Systems Questions and AnswersFundamentals of Database Systems Questions and Answers
Fundamentals of Database Systems Questions and AnswersAbdul Rahman Sherzad
 
Everything about Database JOINS and Relationships
Everything about Database JOINS and RelationshipsEverything about Database JOINS and Relationships
Everything about Database JOINS and RelationshipsAbdul Rahman Sherzad
 
Fal-e-Hafez (Omens of Hafez) Cards in Persian using Java
Fal-e-Hafez (Omens of Hafez) Cards in Persian using JavaFal-e-Hafez (Omens of Hafez) Cards in Persian using Java
Fal-e-Hafez (Omens of Hafez) Cards in Persian using JavaAbdul Rahman Sherzad
 
Web Design and Development Life Cycle and Technologies
Web Design and Development Life Cycle and TechnologiesWeb Design and Development Life Cycle and Technologies
Web Design and Development Life Cycle and TechnologiesAbdul Rahman Sherzad
 
Java Virtual Keyboard Using Robot, Toolkit and JToggleButton Classes
Java Virtual Keyboard Using Robot, Toolkit and JToggleButton ClassesJava Virtual Keyboard Using Robot, Toolkit and JToggleButton Classes
Java Virtual Keyboard Using Robot, Toolkit and JToggleButton ClassesAbdul Rahman Sherzad
 
Java Unicode with Live GUI Examples
Java Unicode with Live GUI ExamplesJava Unicode with Live GUI Examples
Java Unicode with Live GUI ExamplesAbdul Rahman Sherzad
 

Más de Abdul Rahman Sherzad (20)

Iterations and Recursions
Iterations and RecursionsIterations and Recursions
Iterations and Recursions
 
Sorting Alpha Numeric Data in MySQL
Sorting Alpha Numeric Data in MySQLSorting Alpha Numeric Data in MySQL
Sorting Alpha Numeric Data in MySQL
 
PHP Variable variables Examples
PHP Variable variables ExamplesPHP Variable variables Examples
PHP Variable variables Examples
 
Cross Join Example and Applications
Cross Join Example and ApplicationsCross Join Example and Applications
Cross Join Example and Applications
 
Applicability of Educational Data Mining in Afghanistan: Opportunities and Ch...
Applicability of Educational Data Mining in Afghanistan: Opportunities and Ch...Applicability of Educational Data Mining in Afghanistan: Opportunities and Ch...
Applicability of Educational Data Mining in Afghanistan: Opportunities and Ch...
 
Web Application Security and Awareness
Web Application Security and AwarenessWeb Application Security and Awareness
Web Application Security and Awareness
 
Database Automation with MySQL Triggers and Event Schedulers
Database Automation with MySQL Triggers and Event SchedulersDatabase Automation with MySQL Triggers and Event Schedulers
Database Automation with MySQL Triggers and Event Schedulers
 
Mobile Score Notification System
Mobile Score Notification SystemMobile Score Notification System
Mobile Score Notification System
 
Herat Innovation Lab 2015
Herat Innovation Lab 2015Herat Innovation Lab 2015
Herat Innovation Lab 2015
 
Evaluation of Existing Web Structure of Afghan Universities
Evaluation of Existing Web Structure of Afghan UniversitiesEvaluation of Existing Web Structure of Afghan Universities
Evaluation of Existing Web Structure of Afghan Universities
 
PHP Basic and Fundamental Questions and Answers with Detail Explanation
PHP Basic and Fundamental Questions and Answers with Detail ExplanationPHP Basic and Fundamental Questions and Answers with Detail Explanation
PHP Basic and Fundamental Questions and Answers with Detail Explanation
 
Java Applet and Graphics
Java Applet and GraphicsJava Applet and Graphics
Java Applet and Graphics
 
Fundamentals of Database Systems Questions and Answers
Fundamentals of Database Systems Questions and AnswersFundamentals of Database Systems Questions and Answers
Fundamentals of Database Systems Questions and Answers
 
Everything about Database JOINS and Relationships
Everything about Database JOINS and RelationshipsEverything about Database JOINS and Relationships
Everything about Database JOINS and Relationships
 
Fal-e-Hafez (Omens of Hafez) Cards in Persian using Java
Fal-e-Hafez (Omens of Hafez) Cards in Persian using JavaFal-e-Hafez (Omens of Hafez) Cards in Persian using Java
Fal-e-Hafez (Omens of Hafez) Cards in Persian using Java
 
Web Design and Development Life Cycle and Technologies
Web Design and Development Life Cycle and TechnologiesWeb Design and Development Life Cycle and Technologies
Web Design and Development Life Cycle and Technologies
 
Java Virtual Keyboard Using Robot, Toolkit and JToggleButton Classes
Java Virtual Keyboard Using Robot, Toolkit and JToggleButton ClassesJava Virtual Keyboard Using Robot, Toolkit and JToggleButton Classes
Java Virtual Keyboard Using Robot, Toolkit and JToggleButton Classes
 
Java Unicode with Live GUI Examples
Java Unicode with Live GUI ExamplesJava Unicode with Live GUI Examples
Java Unicode with Live GUI Examples
 
Java Regular Expression PART II
Java Regular Expression PART IIJava Regular Expression PART II
Java Regular Expression PART II
 
Java Regular Expression PART I
Java Regular Expression PART IJava Regular Expression PART I
Java Regular Expression PART I
 

Último

Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Celine George
 
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)lakshayb543
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...Postal Advocate Inc.
 
Full Stack Web Development Course for Beginners
Full Stack Web Development Course  for BeginnersFull Stack Web Development Course  for Beginners
Full Stack Web Development Course for BeginnersSabitha Banu
 
How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17Celine George
 
Q4 English4 Week3 PPT Melcnmg-based.pptx
Q4 English4 Week3 PPT Melcnmg-based.pptxQ4 English4 Week3 PPT Melcnmg-based.pptx
Q4 English4 Week3 PPT Melcnmg-based.pptxnelietumpap1
 
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdfLike-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdfMr Bounab Samir
 
Grade 9 Q4-MELC1-Active and Passive Voice.pptx
Grade 9 Q4-MELC1-Active and Passive Voice.pptxGrade 9 Q4-MELC1-Active and Passive Voice.pptx
Grade 9 Q4-MELC1-Active and Passive Voice.pptxChelloAnnAsuncion2
 
Judging the Relevance and worth of ideas part 2.pptx
Judging the Relevance  and worth of ideas part 2.pptxJudging the Relevance  and worth of ideas part 2.pptx
Judging the Relevance and worth of ideas part 2.pptxSherlyMaeNeri
 
ANG SEKTOR NG agrikultura.pptx QUARTER 4
ANG SEKTOR NG agrikultura.pptx QUARTER 4ANG SEKTOR NG agrikultura.pptx QUARTER 4
ANG SEKTOR NG agrikultura.pptx QUARTER 4MiaBumagat1
 
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATIONTHEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATIONHumphrey A Beña
 
ENGLISH6-Q4-W3.pptxqurter our high choom
ENGLISH6-Q4-W3.pptxqurter our high choomENGLISH6-Q4-W3.pptxqurter our high choom
ENGLISH6-Q4-W3.pptxqurter our high choomnelietumpap1
 

Último (20)

TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdfTataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
 
Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17
 
FINALS_OF_LEFT_ON_C'N_EL_DORADO_2024.pptx
FINALS_OF_LEFT_ON_C'N_EL_DORADO_2024.pptxFINALS_OF_LEFT_ON_C'N_EL_DORADO_2024.pptx
FINALS_OF_LEFT_ON_C'N_EL_DORADO_2024.pptx
 
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
 
Raw materials used in Herbal Cosmetics.pptx
Raw materials used in Herbal Cosmetics.pptxRaw materials used in Herbal Cosmetics.pptx
Raw materials used in Herbal Cosmetics.pptx
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
 
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
 
Full Stack Web Development Course for Beginners
Full Stack Web Development Course  for BeginnersFull Stack Web Development Course  for Beginners
Full Stack Web Development Course for Beginners
 
How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17
 
Q4 English4 Week3 PPT Melcnmg-based.pptx
Q4 English4 Week3 PPT Melcnmg-based.pptxQ4 English4 Week3 PPT Melcnmg-based.pptx
Q4 English4 Week3 PPT Melcnmg-based.pptx
 
YOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptx
YOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptxYOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptx
YOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptx
 
OS-operating systems- ch04 (Threads) ...
OS-operating systems- ch04 (Threads) ...OS-operating systems- ch04 (Threads) ...
OS-operating systems- ch04 (Threads) ...
 
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdfLike-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
 
Grade 9 Q4-MELC1-Active and Passive Voice.pptx
Grade 9 Q4-MELC1-Active and Passive Voice.pptxGrade 9 Q4-MELC1-Active and Passive Voice.pptx
Grade 9 Q4-MELC1-Active and Passive Voice.pptx
 
Judging the Relevance and worth of ideas part 2.pptx
Judging the Relevance  and worth of ideas part 2.pptxJudging the Relevance  and worth of ideas part 2.pptx
Judging the Relevance and worth of ideas part 2.pptx
 
ANG SEKTOR NG agrikultura.pptx QUARTER 4
ANG SEKTOR NG agrikultura.pptx QUARTER 4ANG SEKTOR NG agrikultura.pptx QUARTER 4
ANG SEKTOR NG agrikultura.pptx QUARTER 4
 
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
 
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATIONTHEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
 
LEFT_ON_C'N_ PRELIMS_EL_DORADO_2024.pptx
LEFT_ON_C'N_ PRELIMS_EL_DORADO_2024.pptxLEFT_ON_C'N_ PRELIMS_EL_DORADO_2024.pptx
LEFT_ON_C'N_ PRELIMS_EL_DORADO_2024.pptx
 
ENGLISH6-Q4-W3.pptxqurter our high choom
ENGLISH6-Q4-W3.pptxqurter our high choomENGLISH6-Q4-W3.pptxqurter our high choom
ENGLISH6-Q4-W3.pptxqurter our high choom
 

OXUS 20 JAVA Programming Questions and Answers PART II

  • 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 7
  • 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.  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
  • 3. 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
  • 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. }  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
  • 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); } }  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
  • 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 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
  • 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