Join
Blogs
Questions
Videos
Tags
Members
Search
 
 
Create your own blog, earn points and get popular!

Megha's blog

Megha's picture

Java ArrayList foreach

2
Your rating: None Average: 2 (1 vote)
This small Java example shows how to traverse Java ArrayList using foreach loop.
  1. import java.util.ArrayList;
  2.  
  3. //Java ArrayList foreach
  4. public class ArrayListForEachExample {
  5.  
  6.     public static void main(String args[]){
  7.        
  8.         //create new ArrayList
  9.         ArrayList<String> aListColors = new ArrayList<String>();
  10.         aListColors.add("Red");
  11.      
Megha's picture

Java ArrayList Clone

4
Your rating: None Average: 4 (3 votes)
This Java ArrayList clone example shows how to clone Java ArrayList object.
  1. import java.util.ArrayList;
  2. import java.util.Iterator;
  3.  
  4. //Java clone ArrayList example
  5.  
  6. public class JavaCloneArrayListExample {
  7.    
  8.     public static void main(String args[]){
  9.        
  10.         //create original ArrayList object
  11.         ArrayList<Employee> aListOriginal = new ArrayList<Employee&g
Megha's picture

What will happen when you compile and run the following code?

3.5
Your rating: None Average: 3.5 (2 votes)
  1. public class BooleanDemo {
  2.     public static void main(String args[]){
  3.         Boolean bObj = new Boolean("Hello World!");
  4.         System.out.println(bObj);
  5.     }
  6. }
a. Program will not compile
b. Program will throw runtime error
c. Program will print false
d. Program will print true

Answer:
Megha's picture

What will be output on compiling and running the following code?

0
Your rating: None
  1. public class DemoThread extends Thread{
  2.    
  3.     public void run(String msg){
  4.         System.out.println("Thread Started: " + msg);
  5.     }
  6.    
  7.     public static void main(String args[]){
  8.         DemoThread thread = new DemoThread();
  9.         thread.start();
  10.     }    
  11. }
a. Program will not compile
b. Program will compile, but will not run.
Megha's picture

SCJP True False Question Set 1

5
Your rating: None Average: 5 (1 vote)
Q.1 Will this statement compile?

  int i= 1 + '1';

  a. true
b. false


  Q.2 java.util.Set maintains key value pair.
  a. True
b. False
Megha's picture

Which of the following statements are true about a static variable?

0
Your rating: None
a. Static variable retain its value between method calls.
b. Value of static variable can’t be changed once assigned.
c. Variable can’t be static, only method can be.
d. Only one static variable is shared among all objects of the class.
Megha's picture

Given the following classes, which of the following statements will compile without error?

0
Your rating: None
  1. interface myInterface{}
  2. class another implements myInterface{}
  3. class Hello{}
  4.  
  5. public class HelloWorld extends Hello{
  6.  
  7.  public static void main(String argv[]){
  8.  
  9.   HelloWorld hw= new HelloWorld();
  10.   Hello h = new Hello();
  11.   Object obj = new Object();
  12.   myInterface mi = new another();
  13.  
  14.      }
  15. }
a. obj = mi;
b. mi=obj;
c. h=hw;
d. hw=h;
Megha's picture

What will happen when you attempt to compile and run the following code?

0
Your rating: None
  1. public class Demo
  2. {
  3.    int myInt = 1;
  4.    public static void main(String argv[])
  5.    {
  6.        int myInt;
  7.        System.out.println(myInt);
  8.    }
  9. }
a. 1 as output
b. 0 as output
c. Compile time error
d. Run time error
Megha's picture

What will happen when you attempt to compile and run the following code?

0
Your rating: None
  1. public class Demo
  2. {
  3.    public static void main(String args[])
  4.    {
  5.        Long lng = new Long(113);
  6.        System.out.println(lng);
  7.    }
  8. }
a. Compilation error
b. No compilation error but will throw runtime exception when executed.
c. 113
d. None of the above
Megha's picture

What will be the output, when the following code is executed?

0
Your rating: None
  1. public class Demo
  2. {
  3.    public static void main(String args[])
  4.    {
  5.        outer: for(int i = 0; i < 5; ++i)
  6.            inner: for(int j = 0; j < 5; ++j)
  7.            {
  8.                if(j==0)
  9.                break inner;
  10.                System.out.println("j is " + j);
  11.            }
  12.    }
  13. }
a. j is 0
b. j is 3
c. j is 1
Syndicate content