You are on page 1of 43

Question 1

Which of the following package will help to the below program that compiles
without error?

/*Insert Line*/
public class Execute
{
public static void main(String[] args)
{
         Date d = new Date();
     }
}
Question 1
A) import java.util.Date;

B) import java.lang.Date;

C) import java.text.Date;

D) import java.io.Date
Question 2
Assume that the date is 01/23/2014, time is 9H 54M 36S, ISTzone and the day
is Thursday. Then what could be the output of the following program?

import java.util.*;
public class TestSkill 
{
    public static void main(String args[]) 
{
        Date dNow = new Date();
        System.out.println("Current Date: " + (dNow));
    }
}
Question 2
A) Current Date: Jan 23 2014

B) Current Date: Thu Jan 23 2014 09:54

C) Current Date: Thu Jan 23 09:54:36 IST 2014

D) Compilation Error or Runtime Error


Question 3
Assume that the date is 01/23/2014, time is 10H 56M 02S, IST zone and the day
is Thursday. Then what could be the output of the following program?
1 import java.util.*;
2 public class Main
3 {
4     public static void main(String args[]) 
5 {
6         try 
7 {
8             System.out.println(new Date());
9             Thread.sleep(5 * 60 * 10);
10             System.out.println(new Date());
11         } 
12 catch (Exception e) 
13 {
14             System.out.println("Got an exception!");
15         }
16     }
17 }
18
19
20
21
22
Question 3
A) Thu Jan 23 10:56:02 IST 2014
Thu Jan 23 11:01:02 IST 2014

B) Thu Jan 23 10:56:02 IST 2014


Thu Jan 23 11:46:02 IST 2014

C) Thu Jan 23 10:56:02 IST 2014


Thu Jan 23 10:56:05 IST 2014

D) Compilation Error or Runtime Error


1 //Predict the Output
2 import java.util.*;
3 public class Main
4 {
5 public static void main(String[] args)
6 {
7 Calendar cal = Calendar.getInstance();
8 cal.set(Calendar.HOUR, 23);
9 cal.set(Calendar.MINUTE, 58);
10 cal.set(Calendar.SECOND, 59);
11 System.out.print(cal.get(Calendar.HOUR) + ":");
12 System.out.print(cal.get(Calendar.MINUTE) + ":");
13 System.out.print(cal.get(Calendar.SECOND));
14 }
15 }
16
17
18
19
20
21
22
Question 4
A) 23:58:59

B) 11:58:59

C) Output can not determined

D) Compilation Error or Runtime Error


1 //Predict the Output
2 import java.util.*;
3 public class Main
4 {
5 public static void main(String[] args)
6 {
7 Calendar cal = Calendar.getInstance();
8 cal.set(Calendar.HOUR, 73);
9 cal.set(Calendar.MINUTE, 158);
10 cal.set(Calendar.SECOND, 60);
11 System.out.print(cal.get(Calendar.HOUR) + ":");
12 System.out.print(cal.get(Calendar.MINUTE) + ":");
13 System.out.print(cal.get(Calendar.SECOND));
14 }
15 }
16
17
18
19
20
21
22
Question 5
A) 3:39:0

B) 1:39:1

C) 1:39:0

D) Compilation Error or Runtime Error


1 //Predict the Output
2 import java.util.*;
3 public class Main
4 {
5 public static void main(String[] args)
6 {
7 Calendar cal = Calendar.getInstance();
8 cal.set(Calendar.DAY_OF_MONTH, 7);
9 cal.set(Calendar.MONTH, 4);
10 cal.set(Calendar.YEAR, 2015);
11 System.out.println("Week of month "+
12 cal.get(Calendar.WEEK_OF_MONTH));
13 System.out.println("Day of week in month "+
14 (cal.get(Calendar.DAY_OF_WEEK_IN_MONTH)));
15 System.out.println("Day "+ cal.get(Calendar.DAY_OF_MONTH));
16 }
17 }
18
19
20
21
22
Question 6
A) Week of month 2
Day of week in month 2
Day 7
B) Week of month 2
Day of week in month 1
Day 7
C) Week of month 2
Day of week in month 1
Day 7
D) Compilation Error or Runtime Error
1 //Predict the Output
2 import java.util.*;
3 public class Main
4 {
5 public static void main(String[] args)
6 {
7 Calendar cal = Calendar.getInstance();
8 cal.set(Calendar.DAY_OF_MONTH, 7);
9 cal.set(Calendar.MONTH, 4);
10 cal.set(Calendar.YEAR, 2015);
11 System.out.println("Month "+ (Calendar.MAY ==
12 cal.get(Calendar.MONTH) ? "May" : "April"));
13 }
14 }
15
16
17
18
19
20
21
22
Question 7
A) Month April

B) Month May

C) Output can not determined

D) Compilation Error or Runtime Error


1 //Predict the Output
2 import java.util.*;
3 public class Main{
4 public static void main(String[] a){
5 int day = 1;
6 switch (day) {
7 case Calendar.MONDAY :
8 System.out.print("Mon" + getDay());break;
9 case Calendar.TUESDAY :
10 System.out.print("Tues" + getDay());break;
11 case Calendar.WEDNESDAY :
12 System.out.println("Wednes" + getDay());break;
13 case Calendar.THURSDAY :
14 System.out.print("Thurs" + getDay());break;
15 case Calendar.FRIDAY :
16 System.out.print("Fri" + getDay());break;
17 case Calendar.SATURDAY :
18 System.out.print("Satur" + getDay());break;
19 case Calendar.SUNDAY :
20 System.out.print("Sun" + getDay());
21 }
22 }
1 private static String getDay()
2 {
3 return "day";
4 }
5 }
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
Question 8
A) Monday

B) Sunday

C) Output can not determined

D) Compilation Error or Runtime Error


1 //Predict the Output
2 import java.util.*;
3 public class Main
4 {
5 public static void main(String[] args) {
6 GregorianCalendar gc = new GregorianCalendar(2013, 8, 15, 21, 69,
7 55);
8 p(gc, Calendar.YEAR);
9 p(gc, Calendar.MONTH);
10 p(gc, Calendar.DATE);
11 p(gc, Calendar.DAY_OF_WEEK);
12 p(gc, Calendar.DAY_OF_WEEK_IN_MONTH);
13 p(gc, Calendar.DAY_OF_YEAR);
14 p(gc, Calendar.HOUR);
15 p(gc, Calendar.HOUR_OF_DAY);
16 p(gc, Calendar.MINUTE);
17 p(gc, Calendar.SECOND);
18 }
19 static void p(Calendar c, int type) {
20 System.out.print(c.get(type) + "-");
21 }
22 }
Question 9
A) 2013-8-15-1-3-258-10-22-9-55-

B) 2013-8-15-1-3-258-10-21-69-55-

C) 2013-8-15-1-3-3-10-22-9-55

D) Compilation Error or Runtime Error


1 //Predict the Output
2 import java.util.*;
3 public class Main
4 {
5 public static void main(String[] args)
6 {
7 GregorianCalendar gc = new GregorianCalendar(2014, 1, 1);
8 System.out.print(gc.get(Calendar.DAY_OF_WEEK) + "-");
9 for (int i = 0; i < 10; i++)
10 {
11 System.out.print(gc.get(Calendar.DAY_OF_WEEK_IN_MONTH) + "-");
12 gc.add(Calendar.DATE, 3);
13 }
14 }
15 }
16
17
18
19
20
21
22
Question 10
A) 1~1~1~2~2~2~3~3~4~4~1~

B) 7-1-1-1-2-2-3-3-4-4-4-

C) 7~1~1~1~1~1~1~1~2~2~2~

D) Compilation Error or Runtime Error


1 //Predict the Output
2 import java.util.*;
3 public class Main
4 {
5 public static void main(String[] args)
6 {
7 GregorianCalendar gc = new GregorianCalendar(2013, 9, 15, 21, 69,
8 55);
9 p(gc, Calendar.YEAR);
10 p(gc, Calendar.MONTH);
11 p(gc, Calendar.DATE);
12 p(gc, Calendar.HOUR);
13 p(gc, Calendar.MINUTE);
14 p(gc, Calendar.SECOND);
15 }
16 static void p(Calendar c, int type)
17 {
18 System.out.print(c.get(type) + "-");
19 }
20 }
21
22
Question 11
A) 2013-Aug-15-22-09-55-

B) 2013-Sep-15-22-09-55-

C) 2013-9-15-10-9-55-

D) Compilation Error or Runtime Error


1 //Predict the Output
2 import java.util.*;
3 public class Main
4 {
5 public static void main(String[] args)
6 {
7 Calendar now = Calendar.getInstance();
8 TimeZone timeZone = TimeZone.getTimeZone("IST");
9 System.out.println(timeZone.getDisplayName());
10 timeZone = TimeZone.getTimeZone("GMT");
11 now.setTimeZone(timeZone);
12 System.out.println(timeZone.getDisplayName());
13 }
14 }
15
16
17
18
19
20
21
22
Question 12
A) Greenwich Mean Time
India Standard Time

B) India Standard Time


Greenwich Mean Time

C) Greenwich Mean Time


India Standard Time

D) Compilation Error or Runtime Error


Question 13
 How to get difference between two dates?

A)  long diffInMilli = java.time.Duration.between(dateTime1, dateTime2) .


toMillis();
B) long diffInMilli = java.time.difference(dateTime1, dateTime2).toMillis();

C) Date diffInMilli = java.time.Duration.between(dateTime1, dateTime2)


.toMillis();
D) Time diffInMilli = java.time.Duration.between(dateTime1,
dateTime2).toMillis();
Question 14
To create an object and set the date to JUNE 22, 2013, which one of the following
statement should be executed?

A)  $date = Date(“22 JUNE 2013”)

B) $date = new Date(“JUNE 22 2013”)

C) $date = DateTime(“22 JUNE 2013”)

D)  $date = new DateTime(“22 JUNE 2013”)


Question 15
Which of the following statements can be used to add two months to the existing
date?

A) $date->modify(‘+2 months’);

B) $date = modify(‘+2 months’);

C) $date = modify(‘2+ months’);

D) $date->modify(‘2+ months’);
Question 16
 How to convert Date object to String?

A) SimpleDateFormat sdf = new SimpleDateFormat("yyyy-mm-dd"); sdf.parse(new


Date());
B) SimpleDateFormat sdf = new SimpleDateFormat("yyyy-mm-dd");
sdf.format(new Date());
C) SimpleDateFormat sdf = new SimpleDateFormat("yyyy-mm-dd");
new Date().parse();
D) SimpleDateFormat sdf = new SimpleDateFormat("yyyy-mm-dd"); new
Date().format();
Question 17
How to convert a String to a Date object?

A) SimpleDateFormat sdf = new SimpleDateFormat("yyyy-mm-dd"); sdf.parse(new


Date());
B) SimpleDateFormat sdf = new SimpleDateFormat("yyyy-mm-dd");
sdf.format(new Date());
C) SimpleDateFormat sdf = new SimpleDateFormat("yyyy-mm-dd");
new Date().parse();
D) SimpleDateFormat sdf = new SimpleDateFormat("yyyy-mm-dd");
new Date().format();
1 //Predict the Output
2 import java.util.Date;
3 import java.util.Calendar;
4 public class Main
5 {
6 public static void main(String[] args)
7 {
8 Calendar c1 = Calendar.getInstance();
9 c1.set(Calendar.MONTH, 22);
10 c1.set(Calendar.DATE, 05);
11 c1.set(Calendar.YEAR, 1998);
12 Date dateOne = c1.getTime();
13 Calendar c2 = Calendar.getInstance();
14 c2.set(Calendar.MONTH, 06);
15 c2.set(Calendar.DATE, 06);
16 c2.set(Calendar.YEAR, 1995);
17 Date dateTwo = c2.getTime();
18 System.out.println("Are both dates equal: "+
19 dateTwo.equals(dateOne));
20 }
21 }
22
Question 18
A) Output can not determined

B) Are both dates equal: true

C) Are both dates equal: false

D) Compilation Error or Runtime Error


1 //Predict the Output
2 import java.util.*;
3 import java.time.*;
4 public class Main
5 {
6 public static void main(String[] args)
7 {
8 MonthDay tm1 = MonthDay.parse("--10-06");
9 LocalDate dt1 = tm1.atYear(2018);
10 MonthDay tm2 = MonthDay.parse("--12-06");
11 LocalDate dt2 = tm2.atYear(2018);
12 System.out.println(dt1.compareTo(dt2));
13 }
14 }
15
16
17
18
19
20
21
22
Question 19
A) -1

B) -2

C) -3

D) Compilation Error or Runtime Error


1 //Predict the Output
2 import java.time.*;
3 public class Main
4 {
5 public static void main(String[] args)
6 {
7 MonthDay month = MonthDay.parse("--12-12");
8 System.out.println("YearMonth: "+ month.toString());
9 }
10 }
11
12
13
14
15
16
17
18
19
20
21
22
Question 20
A) YearMonth: --12-12

B) It will print current Date

C) Output can not determined

D) Compilation Error or Runtime Error


Programming
Question 1
Write a java program to get the day of the given date using Calendar class.

Sample Input: Sample Output:


22 05 1998 FRIDAY
1 import java.util.*;
2 public class Main
3 {
4 public static void main(String[] args) {
5 Scanner sc = new Scanner(System.in);
6 int dd=sc.nextInt();
7 int mm=sc.nextInt();
8 int yy=sc.nextInt();
9 System.out.print(getDay(dd,mm,yy));
10 }
11 public static String getDay(int day, int month, int year)
12 {
13
14 Calendar cal = Calendar.getInstance();
15 if(month==1)
16 {
17 cal.set(year,0,day);
18 }
19 else
20 {
21 cal.set(year,month-1,day);
22 }
1 int dow = cal.get(Calendar.DAY_OF_WEEK);
2 switch (dow) {
3 case 1:
4 return "SUNDAY";
5 case 2:
6 return "MONDAY";
7 case 3:
8 return "TUESDAY";
9 case 4:
10 return "WEDNESDAY";
11 case 5:
12 return "THURSDAY";
13 case 6:
14 return "FRIDAY";
15 case 7:
16 return "SATURDAY";
17 default:
18 System.out.println("GO To Hell....");
19 }
20 return null;
21 }
22 }
THANK YOU

You might also like