Search This Blog

Showing posts with label Java programming. Show all posts
Showing posts with label Java programming. Show all posts

Wednesday, February 11, 2026

Java Multithreading - 6

 How many times can you start a same Java thread?

a) once only

b) Any number of times 

c) Three times


Created by: Nancy KAN

@11:17 11-Feb-2026


Thursday, October 2, 2025

Java Closeable Interface

 Which of the following is the super interface of Closeable?

a) Close

b) AutoClose

c) AutoCloseable

d) Channel


Created by Nancy K.A.N.

@17:09 02-Oct-2025

Monday, September 15, 2025

Java Garbage Collection

 One of the features of Java is auto-garbage collection. It frees the coder from keeping track of deallocating memory, though you can request garbage collection. 

Which of the following can request garbage collection?

a) Runtime.free()

b) Runtime.getRuntime().gc()

c) System.gc()

d) System.clear()


Created by Nancy K.A.N.

@12:46 15-09-2025

Friday, September 12, 2025

Java Math API - 2

 What will be the output of compiling and executing the following code?







a) -4

b) 4

c) 4.563

d) Compile-time error: Invalid argument type


Created by Nancy K.A.N.

@12:01 12-Sept-2025



Wednesday, September 10, 2025

Java IO - 3

 Which of the following interfaces does the InputStream class implement?

a) Serializable

b) SeekableByteChannel

c) Closeable

d) Flushable


Created by Nancy K.A.N.

@12:36 10-Sept-2025

Monday, September 8, 2025

Java Object duplication

 Which of the following methods can be used to create a copy of the object on which it is called?

a) copy

b) copyObject()

c) clone()

d) duplicate()

e)CloneCurrentObject()


Created by Nancy K.A.N.

@13:32 08-Sept-2025

Tuesday, September 2, 2025

Java Math API - 1

 Which of the following is a superclass of java.lang.Math class?

a) System

b) Process

c) Integer

d) Object


Created by Nancy K.A.N.  

@14:52 02-Sept-2025

Wednesday, June 11, 2025

Java Collections - 1

 Which of the following is the root interface of Java collections?

a) Collections

b) Collection

c) Abstract Collection

d) Concurrent Collection


Created by Nancy N.

@12:38 11-Jun-2026

Wednesday, December 16, 2015

Enhanced for Loop : Collections and Immutability

Enhanced for loop was added in Java SE 1.5. It can be used with Arrays and collections to iterate item-wise/element-wise. It works like read data of very ancient programming language called BASIC... atleast to me it looks like that... You can define read element of the collection element type and do operations on data. The amazing part is whatever you do to the read element, it does not disturb the original collection, that is data in for loop is immutable...
package test;
import java.util.ArrayList;
import java.util.List;
import static java.lang.Math.random;
/**
*
* @author nkan
*/
public class EnhancedForLoop {
public static void main(String[] args) {
List<Integer> intList = new ArrayList<>();
for (int i=0;i<5;i++){
intList.add((int) (random() * 100));
}

System.out.println("Added List elements : "+intList);
/* To perfomr element wise operation */
for(int x : intList){
x++;
System.out.println("x = "+x);
}
/* you have incremented x, may be you are thinking that ArrayList : intList has be modified */
System.out.println("List elements now : "+intList);
/* OOPs... it has not changed...But let us conform it once again by using enhanced for loop */
System.out.println("After Updation : incremented element list is : ");
for(int x : intList){
System.out.println("x = "+x);
}
}
}
When I executed this program :
Added List elements : [98, 14, 30, 34, 43]
x = 99
x = 15
x = 31
x = 35
x = 44
List elements now : [98, 14, 30, 34, 43]
After Updation : incremented element list is : 
x = 98
x = 14
x = 30
x = 34
x = 43

Created and posted by Nancy @ 12/17/2015 2:25 am

Sunday, June 21, 2015

Interesting Facts about JAVA - 2

Did you know ? (These were later removed from java)
1. All exceptions were unchecked
2. unprotect keyword could be used to avoid signaling asynchronous exceptions

Monday, April 30, 2012

Why main() method in Java is static?

Java is a strictly Object-Oriented language. Java does not support open functions like C and C++. In Java, data and methods are written inside a class. Like C and C++, Java applications’ entry point is the main method. The main method is coded inside a Starter class. This is an interesting scenario. A method of a class can be invoked using its object reference. Now the question is, where will you create this object?  An object can be created inside another class, e.g.
public class A {
      public void sayHello() {
              System.out.println(“Hello from Java”);
      }
}

To test class A, I create a StartA class.
public  class StartA {
      public void main(String args[]) { // main needs to be public, as will be invoked from outside the class
               A ob = new A();
               ob.sayHello();
      }
}
To call sayHello() method
Class A’s object is created in StatrtA class.  The sayHello() method is called by the object reference of class A.   Every application’s execution starts from main().  To call main(), an object of the StartA class needs to be created. To create an object of the StartA class, another class is needed…. So,  create another class to create an object of the StartA class, then where can the object be created for that class? Create another class for that, and then what next? It will be a never-ending story.  In this black box, make a hole and let the main() method slip out. This hole can be created by declaring main() as a static method.  A static member belongs to a class and not to the object. By declaring the main() method as static, the JVM can call the main() by its class name, i.e, StartA.main().  Java’s beauty is in its simplicity.
 StartA class can be coded as :
public  class StartA {
      public static void main(String args[]) { // main needs to be static to be called by its class name
               A ob = new A();
               ob.sayHello();
      }
}