Unit-1 Question and Answers
Unit-1 Question and Answers
Java Example:
public class Main {
public sta c void main(String[] args) {
System.out.println("Hello, Java!");
}
}
JavaScript Example:
console.log("Hello, JavaScript!");
Java is suitable for complex, backend systems, whereas JavaScript dominates web front-
end interac vity.
Q3. Explain various places where JavaScript can be placed in HTML by using examples.
JavaScript can be placed in an HTML document in three main ways:
1. Inline JavaScript: JavaScript code is directly wri en inside an HTML tag’s a ribute.
Example:
<bu on onclick="alert('Inline JavaScript')">Click Me</bu on>
2. Internal JavaScript: JavaScript is included within the <script> tag inside the HTML
file.
Example:
<script>
document.write("Internal JavaScript Example");
</script>
3. External JavaScript: JavaScript is wri en in a separate file with a .js extension and
linked using the <script> tag.
Example:
<script src="script.js"></script>
Contents of script.js:
alert("External JavaScript Example");
External scripts are reusable and help maintain cleaner HTML files.
Q5. Explain various Condi onal Statements available in JavaScript by using examples.
JavaScript provides mul ple ways to implement decision-making through condi onal
statements:
1. if Statement: Executes a block of code if the condi on is true. Example:
let age = 20;
if (age >= 18) {
console.log("Eligible to vote");
}
2. if-else Statement: Executes one block if the condi on is true, another if it’s false.
Example:
let age = 16;
if (age >= 18) {
console.log("Adult");
} else {
console.log("Minor");
}
3. if-else if-else Ladder: Mul ple condi ons are checked sequen ally. Example:
let marks = 85;
if (marks >= 90) {
console.log("Grade A");
} else if (marks >= 70) {
console.log("Grade B");
} else {
console.log("Grade C");
}
4. Switch Statement: Suitable for evalua ng mul ple cases of a single expression.
Example:
let color = "red";
switch (color) {
case "red": console.log("Stop"); break;
case "green": console.log("Go"); break;
default: console.log("Invalid color");
}
Each of these constructs ensures flow control in JavaScript programs.
Q8. What do you mean by Objects, Proper es of an Object, and Methods of an Object?
Objects:
Objects are collec ons of key-value pairs, represen ng real-world en es with
a ributes and behaviors.
Syntax:
let objectName = {
property1: value1,
property2: value2,
};
Proper es of an Object:
A ributes that define the object’s characteris cs.
Example:
let car = {
brand: "Tesla",
model: "Model S",
year: 2021,
};
console.log(car.brand); // Output: Tesla
Methods of an Object:
Func ons defined within objects that describe ac ons.
Example:
let car = {
start: func on () {
console.log("Car started");
},
};
car.start(); // Output: Car started
Objects provide a way to encapsulate data and related func onali es, making them
essen al for object-oriented programming.
Dim result
result = AddNumbers(5, 10)
MsgBox "Sum: " & result
Example 2: Func on Without Parameters
vbscript
CopyEdit
Func on GetGree ng()
GetGree ng = "Hello, welcome to VBScript!"
End Func on
Dim message
message = GetGree ng()
MsgBox message
Advantages of Func ons:
Reusability: Func ons can be called mul ple mes.
Modularity: Code is easier to maintain and debug.
Flexibility: Func ons can accept parameters to process different data.
Call ShowAlert()
Differences Between Subrou nes and Func ons:
Feature Subrou ne Func on
Return No Yes
Value
Syntax Sub...End Sub Func on...End Func on
Usage Perform tasks without returning a Perform tasks and return a
value value
Q23. Explain Various Func ons of Dates and Times by Using Examples
VBScript provides several built-in func ons to handle date and me opera ons. These
func ons allow developers to retrieve, manipulate, and format date and me values
efficiently.
Common Date and Time Func ons:
1. Now:
o Returns the current date and me.
o Example:
MsgBox "Current Date and Time: " & Now
2. Date:
o Returns the current date without the me.
o Example:
MsgBox "Today's Date: " & Date
3. Time:
o Returns the current me without the date.
o Example:
MsgBox "Current Time: " & Time
4. Day, Month, Year:
o Extracts the day, month, or year from a date.
o Example:
Dim currentDate
currentDate = Date
MsgBox "Day: " & Day(currentDate)
MsgBox "Month: " & Month(currentDate)
MsgBox "Year: " & Year(currentDate)
5. DateAdd:
o Adds a specified me interval to a date.
o Example:
Dim futureDate
futureDate = DateAdd("d", 5, Date) ' Adds 5 days to today's date
MsgBox "Future Date: " & futureDate
6. DateDiff:
o Calculates the difference between two dates.
o Example:
Dim diff
diff = DateDiff("d", "01/01/2023", "01/15/2023") ' Difference in days
MsgBox "Days Difference: " & diff
7. Weekday:
o Returns the day of the week for a given date (as a number, 1=Sunday to
7=Saturday).
o Example:
MsgBox "Day of the Week: " & Weekday(Date)
8. FormatDateTime:
o Formats a date/ me value based on a specific format.
o Example:
Dim forma edDate
forma edDate = FormatDateTime(Now, vbLongDate)
MsgBox "Forma ed Date: " & forma edDate
Common Date and Time Format Constants:
vbGeneralDate: Default format for date and me.
vbLongDate: Long date format.
vbShortDate: Short date format.
vbLongTime: Long me format.
vbShortTime: Short me format.
Usage Example:
Dim currentDate, forma edTime
currentDate = Now
forma edTime = FormatDateTime(currentDate, vbLongTime)
MsgBox "Full Date: " & currentDate & vbCrLf & "Forma ed Time: " & forma edTime