Click here to load reader
Upload
abdul-rahman-sherzad
View
603
Download
3
Embed Size (px)
DESCRIPTION
OXUS 20 is pleased to offer PART III 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...
Citation preview
https://www.facebook. com/Oxus20
Abdul Rahman Sherzad Page 1 of 6
PART I – Scenario to program and code :
1. Write a method that accept a String named "strInput" as a parameter and returns every other character of that String parameter starting with the first character.
M e t h o d I : - U s i n g L o o p
public static String everyOther(String strInput) { String output = ""; for (int index = 0; index < strInput.length(); index += 2) { output += strInput.charAt(index); } return output; }
M e t h o d I I : - U s i n g R e g u l a r E x p r e s s i o n
public static String everyOther(String strInput) { return strInput.replaceAll("(.)(.)", "$1"); }
Tips:
Each and every characters of the given String parameter needs to be scanned and
processed. Therefore, we need to know the number of characters in the String.
strInput.length();
1st character is needed that is why int index = 0; we don't need the 2nd character that
is why we incremented index by 2 as follow: index += 2
strInput.charAt(index); reading the actual character and concatenated to the output
variable.
Finally System.out.println( everyOther("SshHeErRzZaAdD") ); // will print Sherzad
Tips:
In Regular Expression . matches any character
In Regular Expression () use for grouping
strInput.replaceAll("(.)(.)", "$1"); // in the expression (.)(.), there are
two groups and each group matches any character; then, in the replacement string,
we can refer to the text of group 1 with the expression $1. Therefore, every other
character will be returned as follow:
System.out.println( everyOther("SshHeErRzZaAdD") ); // will print Sherzad
https://www.facebook. com/Oxus20
Abdul Rahman Sherzad Page 2 of 6
2. Write a method named reverseString that accepts a String parameter and returns the reverse of the given String parameter.
M e t h o d I : - U s i n g L o o p
public static String reverseString(String strInput) { String output = ""; for (int index = (strInput.length() - 1); index >= 0; index--) { output += strInput.charAt(index); } return output; }
M e t h o d I I : - U s i n g r e v e r s e ( ) m e t h o d o f S t r i n g B u i l d e r / S t r i n g B u f f e r c l a s s
public static String reverseString(String strInput) { if ((strInput == null) || (strInput.length() <= 1)) { return strInput; } return new StringBuffer(strInput).reverse().toString(); }
M e t h o d I I I : - U s i n g s u b s t r i n g ( ) a n d R e c u r s i o n
public static String reverseString(String strInput) { if ( strInput.equals("") ) return ""; return reverseString(strInput.substring(1)) + strInput.substring(0, 1); }
Tips:
Each and every characters of the given String parameter needs to be scanned and
processed. Therefore, we need to know the number of characters in the String.
strInput.length();
Since index ranges from 0 to strInput.length() - 1; that is why we read each and every
character from the end int index = (strInput.length() - 1);
Finally System.out.println( reverseString("02SUXO") ); // will print OXUS20
Tips:
Both StringBuilder and StringBuffer class have a reverse method. They work pretty much
the same, except that methods in StringBuilder are not synchronized.
public String substring(int beginIndex) public String substring(int beginIndex, int endIndex)
https://www.facebook. com/Oxus20
Abdul Rahman Sherzad Page 3 of 6
3. Write a method to generate a random number in a specific range. For instance, if the given range is 15 - 25, meaning that 15 is the smallest possible value the random number can take, and 25 is the biggest. Any other number in between these numbers is possible to be a value, too.
M e t h o d I : - U s i n g R a n d o m c l a s s o f j a v a . u t i l p a c k a g e
public static int rangeInt(int min, int max) { java.util.Random rand = new java.util.Random(); int randomOutput = rand.nextInt((max - min) + 1) + min; return randomOutput; }
M e t h o d I I : - U s i n g M a t h . r a n d o m ( ) m e t h o d
public static int rangeInt(int min, int max) {
return (int) ( Math.random() * ( (max – min) + 1 ) ) + min;
}
Tips:
The nextInt(int n) method is used to get an int value between 0 (inclusive) and the
specified value (exclusive).
Consider min = 15, max = 25, then
o rand.nextInt(max - min) // return a random int between 0 (inclusive) and 10
(exclusive). Interval demonstration [0, 10)
o rand.nextInt((max - min) + 1) // return a random int 0 and 10 both inclusive
o rand.nextInt((max - min) + 1) + min // return a random int 0 and 10
both inclusive + 15;
if the return random int is 0 then 0 + 15 will be 15;
if the return random int is 10 then 10 + 15 will be 25;
if the return random int is 5 then 5 + 15 will be 20;
Hence the return random int will be between min and max value both
inclusive.
Note:
In practice, the Random class is often preferable
to Math.random().
https://www.facebook. com/Oxus20
Abdul Rahman Sherzad Page 4 of 6
PART I I – Single Choice and Mult ip le Choice s:
1. Which of the following declarations is correct?
[A] boo lean b = TRUE ;
[B] byte b = 255 ;
[C] Str ing s = "nu l l " ;
[D] int i = new Integer("56") ;
2. Consider the following program:
import oxus20Library.*; public class OXUS20 { public static void main(String[] args) { // The code goes here ... } }
What is the name o f the java f i le conta in ing th is program?
A. oxus20Library . java
B. OXUS20. java
C. OXUS20
D. OXUS20.c lass
E . Any f i le name w ith the java su f f ix is accepted
3. Consider the following code snippet public class OXUS20 { public static void main(String[] args) { String river = new String("OXUS means Amu Darya"); System.out.println(river.length()); } }
What is pr in ted?
A. 17 B. OXUS means Amu Darya C. 20 E . r iver
[A] boolean b = TRUE; // JAVA is Case Sensitive!
[B] byte b = 255; // Out of range MIN_VALUE = -128 and
MAX_VALUE = 127
[C] String s = "null"; // Everything between double quotes ""
considered as String
[D] int i = new Integer("56"); // The string "56" is converted
to an int value.
Each JAVA source file can contain only one
public class.
The source file's name has to be the name of
that public class. By convention, the
source file uses a .java filename extension
https://www.facebook. com/Oxus20
Abdul Rahman Sherzad Page 5 of 6
4. A constructor
A. must have the same name as the c lass i t is dec lared w ith in .
B. is used to create ob jects .
C. may be dec lared pr ivate
D. A l l the above
5. Which of the following may be part of a class definition?
A. instance variables
B. instance methods
C. constructors
D. All of the above
E. None of the above
A . A c o n s t r u c t o r m u s t h a v e t h e s a m e n a m e a s t h e c l a s s i t i s d e c l a r e d w i t h i n .
public class OXUS20 { public OXUS20() { } }
B . C o n s t r u c t o r i s u s e d t o c r e a t e o b j e c t s
OXUS20 amu_darya = new OXUS20();
C . C o n s t r u c t o r m a y b e d e c l a r e d p r i v a t e // private constructor is off
course to restrict instantiation of the class. Actually a good
use of private constructor is in Singleton Pattern.
Following is a sample of class declaration:
class MyClass {
Field // class variables or instance variables
constructor, and // class constructor or overloaded constructors
method declarations // class methods or instance methods
}
https://www.facebook. com/Oxus20
Abdul Rahman Sherzad Page 6 of 6
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