May 18

Core java quiz questions on identifiers

Identifiers:

1) An identifier must start with letter, underscore or $.

2) An identifier cannot start with number.

3) An identifier cannot be a reserved keyword in java.

4) we can start an identifier with number after first character.

5) Identifiers in java are case sensitive.

 

[mtouchquiz 1]

May 12

Sorting array using sort() method in java

[code lang=”java”]import java.util.Arrays;

public class SortArrayExample {

public static void main(String[] args) {

String colors[] = { "Red", "Green", "Blue" };

System.out.println("unsorted array");

for (String color : colors) {

System.out.println(color);

}
Arrays.sort(colors);

System.out.println("sorted array");

for (String color1 : colors) {

System.out.println(color1);

}

}

}
[/code]

Output:

unsorted array

Red

Green

Blue

sorted array

Blue

Green

Red


					
May 09

java programme to check a number is palindrome or not

[code lang=”java”]package javaexampleprograms;

public class PalindromeExample {

public static void main(String[] args) {

Integer number = 121;
if (number.equals(PalindromeExample.checkPalindrome(number)))

{
System.out.println(number + " is a palindrome number");
}

else {
System.out.println(number + " is not a palindrome number");
}

}

private static Integer checkPalindrome(Integer number) {

int reverse = 0;

while (number > 0) {

reverse = reverse * 10 + number % 10;
number = number / 10;

}
return reverse;

}

}
[/code]

May 08

String reverse without using reverse method

[code lang=”java”]public class StringReverse {

public static void main(String[] args) {

String str = "Iam string";

String reverseString = reverse(str);

System.out.println(reverseString);

}

private static String reverse(String str) {

StringBuilder stb = new StringBuilder();

char[] stringChars = str.toCharArray();

for (int i = stringChars.length – 1; i >= 0; i–) {

stb.append(stringChars[i]);

}

return stb.toString();

}
}
[/code]