Test Your Java Knowledge (10)
class Blog implements Runnable
{
public void run(){}
}
class Main
{
public static void main (String[] args) throws Exception
{
Thread t1 = new Thread(); //1
Thread t2 = new Thread(new Blog()); //2
Thread t3 = new Thread(new Blog(), "Thread 3"); //3
Thread t4 = new Thread("Thread 4"); //4
Thread t5 = new Thread("Thread 5", 5); //5
Thread t6 = new Thread("Thread 6", new Blog()); //6
}
}
Which of the following lines will not compile? Select two choices
a. Line 1
b. Line 2
c. Line 3
d. Line 4
e. Line 5
f. Line 6
Continue reading »
Test Your Java Knowledge (9)
Before which of the following can the “synchronized” keyword be placed, without causing a compilation error? Choose at least one answer.
a. class methods
b. instance methods
c. any block of code within a method
d. variables
e. a class
Filed under Java, Java SE, Test Your Java Knowledge | Comment (0)
Test Your Java Knowledge (8)
Which of the following is a method of the Locale class? Choose one answer.
a. getLanguage
b. getVariant
c. getCountry
d. All of the above
Filed under Java, Java SE, Test Your Java Knowledge | Comment (0)
Test Your Java Knowledge (7)
Book b1 = new Book();
Book b2 = new Book();
c(b1);
b1=null;
b2=b1;
d();
Assume that Book is a class which defines two methods c() and d(). How many objects would be eligible for garbage collection by the time the method d() is invoked? Choose one answer.
a. 1
b. 2
c. 0
d. The given information is insufficient
Test Your Java Knowledge (6)
What results from running the code below?
int i = -7;
int j = -4;
System.out.println(i % j);
Choose one answer.
a. 0
b. 3
c. -3
d. A compiler error saying \”%\” operator .
Test Your Java Knowledge (5)
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
class Fruits
{
int f=2;
Fruits(int f)
{
this.f=f;
}
}
class Apple extends Fruits implements Serializable
{
int a=5;
Apple(int a)
{
super(a);
this.a=a;
}
}
class SerializeTest
{
public static void main(String[]args) throws Exception
{
File file=new File("file.txt");
ObjectOutputStream oos=new ObjectOutputStream(new FileOutputStream(file));
oos.writeObject(new Apple(2));
oos.close();
ObjectInputStream ois=new ObjectInputStream(new FileInputStream(file));
Apple apple=(Apple)ois.readObject();
System.out.println(apple.a);
ois.close();
}
}
What is the result of compiling and running the following code? Choose one answer.
a. Code does not compile because class Fruits does not implement Serializable.
b. Code does not compile because class Apple does not have a no-arguments constructor.
c. Code compiles fine but causes an exception at runtime.
d. Code compiles and runs fine, printing 5.
e. Code compiles and runs fine, printing 2.
Test Your Java Knowledge (4)
interface Iman { int iman=0; }
class People implements Iman
{
int iman=Iman.iman+1;
}
class Muslim extends People
{
int iman=Iman.iman+2;
static void printAll (People obj)
{
public static void main(String[] args)
{
Muslim muslim = new Muslim();
People people = new Muslim();
Iman iman = new Muslim();
printAll(muslim);
printAll(people);
printAll(iman);
}
}
What will be the result of an attempt to compile and run the following program? Choose one answer.
a. Compiler Error
b. Prints 000
c. Prints 222
d. Prints 210
e. None of the above
Encrypt Text Using Qwerty Algorithm
Qwerty algorithm is created by me and my classmate, Dwindy Stanza.
Example:
ENCRYPTION
The plain text is MISA.
- M is in index 12 in the alphabet and index 0 in plain text. We must add value of index in the alphabet and plain text to get the value of the index in Qwerty. 12 + 0 = 12. So, the encrypted letter is D.
- I is in index 8 in the alphabet and index 1 in plain text. 8 + 1 = 9. So, the encrypted letter is P.
- S is in index 18 in the alphabet and index 2 in plain text. 18 + 2 = 20. So, the encrypted letter is X.
- A is in index 0 in the alphabet and index 3 in plain text. 0 + 3 = 3. So, the encrypted letter is R.
The encrypted text is DPXR.
DECRYPTION
The encrypted text is DPXR.
- D is in index 0 in encrypted text and index 12 in Qwerty. We have to reduce value of index in Qwerty by value of index in encrypted text to get the value of the index in the alphabet. 12 - 0 = 12. So, the plain text is M.
- P is in index 1 in encrypted text and index 9 in Qwerty. 9 - 1 = 8. So, the plain text is I.
- X is in index 2 in encrypted text and index 20 in Qwerty. 20 - 2 = 18. So, the plain text is S.
- R is in index 3 in encrypted text, index 3 in Qwerty. 3 - 3 = 0. So, the plain text is A.
The plain text is MISA.
This is the source code:
Continue reading »
Transpose Matrix in Java
import java.util.Scanner;
/**
*
* @author Iman
*/
public class TranposeMatrix{
public static void main(String[] args){
int row, column;
int[][] matrix1, matrix2;
Scanner scan = new Scanner(System.in);
System.out.print("Enter number of rows: ");
row = scan.nextInt();
System.out.print("Enter number of columns: ");
column = scan.nextInt();
matrix1 = new int[row][column];
matrix2 = new int[column][row];
for(int a = 0; a < row; a++){
System.out.println("\nEnter " + column + " elements for row " + (a + 1) + ": ");
for(int b = 0; b < column; b++){
matrix1[a][b] = scan.nextInt();
}
}
for(int c = 0; c < row; c++){
for(int d = 0; d < column; d++){
matrix2[d][c] = matrix1[c][d];
}
}
System.out.println("\nThe original matrix is :\n");
for(int e = 0; e < row; e++){
for(int f = 0; f < column; f++){
System.out.print(matrix1[e][f]+" ");
}
System.out.println();
}
System.out.println("\nThe tranpose matrix is :\n");
for(int e = 0; e < column; e++){
for(int f = 0; f < row; f++){
System.out.print(matrix2[e][f]+" ");
}
System.out.println();
}
}
}
Filed under Console, Java, Java SE | Comment (0)
What is the Difference between Abstract Classes and Interfaces?
Abstract Classes
- Abstract classes can have a data type with all types of data
- An abstract class can contain concrete methods and / or abstract methods.
- Abstract classes are used only when there is a “is-a” type of relationship between the classes.
- We cannot extend more than one abstract class.
- Abstract class can implemented some methods also.
- With abstract classes, we are grabbing away each class’s individuality.