forked from alc-cohort1/GroupWork
-
Notifications
You must be signed in to change notification settings - Fork 0
/
exercisesampleQuestions
194 lines (91 loc) · 3.36 KB
/
exercisesampleQuestions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
1. What is the HTML tag under which one can write the JavaScript code?
A) <javascript>
B) <scripted>
C) <script>
D) <js>
Ans: Option C
Explanation: If we want to write a JavaScript code under HTML tag, you will have to use the “script” tag.
2. Choose the correct JavaScript syntax to change the content of the following HTML code.
<p id="geek">GeeksforGeeks</p>
A) document.getElement(“geek”).innerHTML=”I am a Geek”;
B) document.getElementById(“geek”).innerHTML=”I am a Geek”;
C) document.getId(“geek”)=”I am a Geek”;
D) document.getElementById(“geek”).innerHTML=I am a Geek;
Ans: B
Explanation: The correct syntax to access the element is document.getElementById(“geek”). Here we want to access the content written under that id, so we used .innerHTML to specify that and finally we replaced the content with whatever is written inside the quotes.
3. Which of the following is the correct syntax to display “GeeksforGeeks” in an alert box using JavaScript?
A. alertbox(“GeeksforGeeks”);
B. msg(“GeeksforGeeks”);
C. msgbox(“GeeksforGeeks”);
D. alert(“GeeksforGeeks”);
Ans: D
Explanation: To display any text in the alert box, you need to write it as alert(“GeeksforGeeks”);.
4. What is the correct syntax for referring to an external script called “geek.js”?
A. <script src=”geek.js”>
B. <script href=”geek.js”>
C. <script ref=”geek.js”>
D. <script name=”geek.js”>
Ans: A
Explanation: The “src” term is used to refer to any JavaScript file.
5. The external JavaScript file must contain <script> tag. True or False?
A. True
B. False
Ans: B
Explanation: It is not necessary for any external javascript file to have <script> tag.
6. Predict the output of the following JavaScript code.
filter_none
<script type="text/javascript">
a = 8 + "8";
document.write(a);
</script>
A) 16
B) Complilation Error
C) 88
D) Run Time Error
Ans: Option C
Explanation: In the above given code, 8+”8″ have first integer and second string data types. Rather than adding the two numbers, it concatenated the two.
7. Predict the output of the following JavaScript code.
<script type="text/javascript">
var a="GeeksforGeeks";
var x=a.lastIndexOf("G");
document.write(x);
</script>
A) 8
B) 0
C) 9
D) Error
Ans: A
Explanation: The index starts with 0 in JavaScript. Here, x searches for the last occurance of “G” in the text.
8. Which of the following is not a reserved word in JavaScript?
A. interface
B. throws
C. program
D. short
Ans: C
Explanation: In JavaScript, interface, throws and short are reserved keywords.
9. Predict the output of the following JavaScript code.
filter_none
<script type="text/javascript" language="javascript">
var a = "GeeksforGeeks";
var result = a.substring(4, 5);
document.write(result);
</script>
A. sf
B. ks
C. s
D. k
Ans: C
Explanation: The substring command selects the substring starting from 4 to 5, excluding the 5th index. The indexing starts from 0. So, the output here is just “s” rather than sf.
10. Predict the output of the following JavaScript code.
<script type="text/javascript" language="javascript">
var x=5;
var y=6;
var res=eval("x*y");
document.write(res);
</script>
A. “30”
B. 30
C. 5*6
D. “5*6”
Ans: B
Explanation: eval command will evaluate the operation. Here it is 5*6=30.