Question : Given the code fragment. What is the result? 1. 10 : 10 2. 5 : 5 3. 5 : 10
4. Compilation fails
Correct Answer : 1 Explanation: As static variable can be shared across class instances. Hence, each object changeCount call will increase same variable. First call will make value 5 and then second call will increase same variable by 5. So final result would be 10.
If you declare any variable as static, it is known static variable.
The static variable can be used to refer the common property of all objects (that is not unique for each object) e.g. company name of employees, college name of students etc. The static variable gets memory only once in class area at the time of class loading. Advantage of static variable
It makes your program memory efficient (i.e it saves memory).
Question : Given the code fragment:
7. StringBuilder sb1 = new StringBuilder("Duke"); 8. String str1 = sb1.toString(); 9. //Insert code here 10. System.out.println(str1==str2);
Which code fragment, when inserted at line 9, enables the code to print true?
1. date1=2014-06-20 date2=2014-06-20 date3=2014-06-20 2. date1=06/20/2014 date1=2014-06-20 date1=Jun 20, 2014 3. Compilation Fails 4. A DateParseException is thrown at runtime.
Correct Answer : 1 Explanation: LocalDate : A date without a time-zone in the ISo-8601 calendar system, such as 2007-12-03. LocalDate is an immutable date-time object that represents a date, often viewed as year-month-day. other date fields, such as day-of-year, day-of-week and week-of-year, can also be accessed. For example, the value "2nd october 2007" can be stored in a LocalDate.
This class does not store or represent a time or time-zone. Instead, it is a description of the date, as used for birthdays. It cannot represent an instant on the time-line without additional information such as an offset or time-zone.
The ISo-8601 calendar system is the modern civil calendar system used today in most of the world. It is equivalent to the proleptic Gregorian calendar system, in which today's rules for leap years are applied for all time. For most applications written today, the ISo-8601 rules are entirely suitable. However, any application that makes use of historical dates, and requires them to be accurate will find the ISo-8601 approach unsuitable.
This is a value-based class; use of identity-sensitive operations (including reference equality (==), identity hash code, or synchronization) on instances of LocalDate may have unpredictable results and should be avoided. The equals method should be used for comparisons.
public static LocalDate now() obtains the current date from the system clock in the default time-zone. This will query the system clock in the default time-zone to obtain the current date.
Using this method will prevent the ability to use an alternate clock for testing because the clock is hard-coded.
Returns: the current date using the system clock and default time-zone, not null
public static LocalDate of(int year, int month, int dayofMonth) obtains an instance of LocalDate from a year, month and day. This returns a LocalDate with the specified year, month and day-of-month. The day must be valid for the year and month, otherwise an exception will be thrown.
Parameters: year - the year to represent, from MIN_YEAR to MAX_YEAR month - the month-of-year to represent, from 1 (January) to 12 (December) dayofMonth - the day-of-month to represent, from 1 to 31 Returns: the local date, not null
public static LocalDate parse(CharSequence text, DateTimeFormatter formatter) obtains an instance of LocalDate from a text string using a specific formatter. The text is parsed using the formatter, returning a date.
Parameters: text - the text to parse, not null formatter - the formatter to use, not null Returns: the parsed local date, not null
public final class DateTimeFormatter extends object Formatter for printing and parsing date-time objects. This class provides the main application entry point for printing and parsing and provides common implementations of DateTimeFormatter:
Using predefined constants, such as ISo_LoCAL_DATE Using pattern letters, such as uuuu-MMM-dd Using localized styles, such as long or medium More complex formatters are provided by DateTimeFormatterBuilder.
The main date-time classes provide two methods - one for formatting, format(DateTimeFormatter formatter), and one for parsing, parse(CharSequence text, DateTimeFormatter formatter).
For example:
LocalDate date = LocalDate.now(); String text = date.format(formatter); LocalDate parsedDate = LocalDate.parse(text, formatter);
public static final DateTimeFormatter ISo_LoCAL_DATE The ISo date formatter that formats or parses a date without an offset, such as '2011-12-03'. This returns an immutable formatter capable of formatting and parsing the ISo-8601 extended local date format. The format consists of:
Four digits or more for the year. Years in the range 0000 to 9999 will be pre-padded by zero to ensure four digits. Years outside that range will have a prefixed positive or negative symbol. A dash Two digits for the month-of-year. This is pre-padded by zero to ensure two digits. A dash Two digits for the day-of-month. This is pre-padded by zero to ensure two digits. The returned formatter has a chronology of ISo set to ensure dates in other calendar systems are correctly converted. It has no override zone and uses the STRICT resolver style.
Related Questions
Question : What is the result?` 1. 97 98 99 100 null null null 2. 97 98 99 100 101 102 103 3. Compilation Fails. 4. A NullPointerException is thrown at runtime. 5. An ArraylndexoutofBoundsException is thrown at runtime.
Question : Given the code fragment: What is the result? 1. Reading Card Card 2. Compilation fails only at line n1. 3. Compilation fails only at line n2. 4. Compilation fails only at line n3. 5. Compilation fails at both line n2 and line n3.